All for Joomla All for Webmasters

Java Date

The java.util, java.sql and java.text packages contains classes for representing date and time. Following classes are important for dealing with date in java.

  • java.util.Date class
  • java.sql.Date class
  • java.text.DateFormat class
  • java.text.SimpleDateFormat class
  • java.util.Calendar class
  • java.util.GregorianCalendar class
  • java.sql.Time class
  • java.sql.Timestamp class
  • java.util.TimeZone class

Get Current Date

There are 4 ways to print current date in java.

1st way:

  1. java.util.Date date=new java.util.Date();
  2. System.out.println(date);

Output:

Wed Mar 27 08:22:02 IST 2015

2nd way:

  1. long millis=System.currentTimeMillis();
  2. java.util.Date date=new java.util.Date(millis);
  3. System.out.println(date);

Output:

Wed Mar 27 08:22:02 IST 2015

3rd way:

  1. long millis=System.currentTimeMillis();
  2. java.sql.Date date=new java.sql.Date(millis);
  3. System.out.println(date);

Output:

2015-03-27

4th way:

  1. Date date=java.util.Calendar.getInstance().getTime();
  2. System.out.println(date);

Output:

Wed Mar 27 08:22:02 IST 2015
PinIt
submit to reddit

Leave a Reply

Top