Showing posts with label SimpleDateFormat. Show all posts
Showing posts with label SimpleDateFormat. Show all posts

Tuesday, 8 October 2013

date difference in mysql , date difference between two date in java


Here we calculate date difference by mysql or java in both way :-



MYSQL

DATEDIFF() Functions returns expr1 – expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

SELECT DATEDIFF('2013-10-30','2013-09-11') from dual; --  will return 49 days 
SELECT DATEDIFF('2013-09-30','2013-10-11') from dual; --   will return -11 days 

Java

package com.javastoreroom.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateDiffrence {

 public static void main(String[] args) {

  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  String dateStart = "2013-09-05";
  String dateStop = "2013-10-08";

  try {
   Date date = format.parse(dateStart);
   Date date1 = format.parse(dateStop);

   long time = date1.getTime() - date.getTime();
   long day = time / (24 * 60 * 60 * 1000);
   System.out.println("count total day ------- :"+day); // line print number of day between these two date count total day ------- :33

  } catch (ParseException e) {
   e.printStackTrace();
  }

 }

}

DONE :)

Thursday, 19 September 2013

Compare 2 Dates , Convert String into Date , Convert Date into String - in java SimpleDateFormat


Here i m discuss SimpleDateFormat class SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> String), parsing (String -> date). It allows you to choosing any user-defined date-time formatting patterns.

Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800

refer this link know to more...
public class SimpleDateFormat extends DateFormat



SimpleDateFormatPattern .Java Sample Code


public static void main(String[] args) throws ParseException {

  SimpleDateFormat format = null;
  String startDate = "2013-01-08 12:10:56";
  String endDate = "2013-12-09 10:10:56";
  
  System.out.println("<<<<<<<<<<<<<<--------Compare 2 Dates------------>>>>>>>>>>>>>>>>>>>>"); 
  new SimpleDateFormatPattern().compareTwoDate( format , startDate , endDate);
  
         System.out.println("<<<<<<<<<<<<<<--------Convert String into Date------->>>>>>>>>>>>>>>>>"); 
  new SimpleDateFormatPattern().convertStringToDate(format , startDate , endDate);
  
         System.out.println("<<<<<<<<<<<<<<--------Convert Date into String------>>>>>>>>>>>>>>>>>>"); 
         new SimpleDateFormatPattern().convertDateToString(format);
 }

Compare 2 Dates
private void compareTwoDate(SimpleDateFormat format, String startDate, String endDate) {
  Date date1 , date2;
  format = new SimpleDateFormat("yyyy-MM-dd");
  try {
   date1 = format.parse(startDate);
   date2 = format.parse(endDate);
   
   if (date1.compareTo(date2) < 0) 
    System.out.println("Start Date is before than End Date");
   
   else if(date1.compareTo(date2) >0)
    System.out.println("Start Date is after than End Date");
   
   else
    System.out.println("Start Date and End Date both are equal");
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
Convert String into Date
private void convertStringToDate(SimpleDateFormat format, String startDate, String endDate) {
  format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  try {
   Date startDates =  format.parse(startDate);
   Date endDates = format.parse(endDate);
   
   System.out.println("String to date    :"+startDates);
   System.out.println("String to date    :"+endDates);
   
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
Convert Date into String
private void convertDateToString(SimpleDateFormat format) {
  format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
   Date date = new Date();
   String todayDate = format.format(date);
   System.out.println("Date to String    :" +todayDate);
 }

after executing SimpleDateFormatPattern.java following result shown :-