Date

Java provides Date Class in java.util.Date package.This class encapsulate current Date and time. When we declare a date object it acquires current date and time. consider below example.


import java.util.Date;

public class MyDate {

public static void main(String[] args)
{
Date date = new Date();
System.out.println(date);

}

}

output :
Mon Mar 28 14:53:03 IST 2016

We can see the program has printed the current date. But generally do we use this format? No. we use date format like ’02-12-2016′ or ’21 March 2002′ like that. so in order to get a date in this format we must understand some notations for date

Notation Description Example
 dd  day in digit  02
 E /EE / EEE  day in words (partial)  Mon
 EEEE  day in words (Full)  Monday
 MM (m capital)  Month in digit  03
 MMM  Month in words (partial)  Mar
 MMMM  Month in words (Full)  March
 M  Month in a single digit when month is single digit. (if we use MM then it gives 02 otherwise 2)  2
 yy  year in 2 digit  16
 yyyy  year in 4 digit  2016
 hh  hours (24 hour clock)  11
 mm  minutes  23
 ss  Seconds  11

 

To use above formats we will need SimpleDateFormat class which is in java.text.SimpleDateFormat package. let us see below example in order to see how can we use this.


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

public class MyDate {

public static void main(String[] args)
{
System.out.println("The Given date is");
Date date = new Date();
System.out.println(date);
SimpleDateFormat sd= new SimpleDateFormat("dd/MM/yyyy");
System.out.println("The Formatted date is");
System.out.println(sd.format(date));
}

}

Output:
The Given date is
Mon Mar 28 15:32:28 IST 2016
The Formatted date is
28/03/2016

sd.format() return a string in required format.

Here we have seen how to convert a data into a given format. but suppose if i am given a date in string format and i wanted it to convert in a date format then what do i do. please consider below example

public class MyDate {

public static void main(String[] args) throws ParseException
{
String str = "30-2-2016";
System.out.println("The Given date in string is : "+ str);
Date date;
SimpleDateFormat sd= new SimpleDateFormat("dd-MM-yyyy");
System.out.println("The Formatted date is");
System.out.println(sd.parse(str));
}

}

 

Output:
The Given date in string is : 30-2-2016
The Formatted date is
Tue Mar 01 00:00:00 IST 2016

Finding a date difference between 2 dates


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

public class DateDiffernce {
 
 public static void main(String[] args) throws ParseException
 {
 String startDate = "06/04/2016 09:29:58";
 String stopDate = "01/06/2016 10:31:48";
 SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
 
 Date d1 ;
 Date d2 ;
 
 d1 = format.parse(startDate);
 d2 = format.parse(stopDate);

long diff = d2.getTime() - d1.getTime(); //difference in milliseconds

long diffSeconds = diff / 1000 % 60;
 long diffMinutes = diff / (60 * 1000) % 60;
 long diffHours = diff / (60 * 60 * 1000) % 24;
 long diffDays = diff / (24 * 60 * 60 * 1000);
 
 System.out.print(diffDays + " days, ");
 System.out.print(diffHours + " hours, ");
 System.out.print(diffMinutes + " minutes, ");
 System.out.print(diffSeconds + " seconds.");
 
 }
 
}

output:
56 days, 1 hours, 1 minutes, 50 seconds


Search

Subscribe to YouTube

Training on Selenium

Like us on facebook

Jobs

Jobs

Cooking with Amol