In this article, we are going to learn about get system date and time using Java.
Introdruction
To get system date and time we are using SimpleDateFormat. In this we use the java classes and constructors and we learn about format and parse.
SimpleDateFormat
In this java.text.SimpleDateFormat class provides format and parse date and time in java. The formatting means converting date to string and parsing means converting string to date.
import java.text.SimpleDateFormat; import java.util.Date;
Constructors:
- SimpleDateFormat(String pattern_arg) : Constructs a Simple Date Format using the given pattern – pattern_arg, default date format symbols for the default FORMAT locale.
- SimpleDateFormat(String pattern_arg, Locale locale_arg) : Constructs a Simple Date Format using the given pattern – pattern_arg, default date format symbols for the given FORMAT Locale – locale_arg.
- SimpleDateFormat(String pattern_arg, DateFormatSymbols formatSymbols) : Constructs a SimpleDateFormat using the given pattern – pattern_arg and date format symbols.
Java SimpleDateFormat Example: Date to String
Now we are going to see simple example to format date in java using java.text.SimpleDateFormat class.
import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateFormatExample { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); String strDate= formatter.format(date); System.out.println(strDate); } }
Output:
13/04/2015
Java SimpleDateFormat Example: String to Date
Now we are going to see simple example to convert string into date using java.text.SimpleDateFormat class.
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateFormatExample3 { public static void main(String[] args) { SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); try { Date date = formatter.parse("31/03/2015"); System.out.println("Date is: "+date); } catch (ParseException e) {e.printStackTrace();} } }
Output:
Date is: Tue Mar 31 00:00:00 IST 2015
Format date and time in java
Let’s see the full example to format date and time in java using java.text.SimpleDateFormat class.
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class SimpleDateFormatExample2 { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); String strDate = formatter.format(date); System.out.println("Date Format with MM/dd/yyyy : "+strDate); formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss"); strDate = formatter.format(date); System.out.println("Date Format with dd-M-yyyy hh:mm:ss : "+strDate); formatter = new SimpleDateFormat("dd MMMM yyyy"); strDate = formatter.format(date); System.out.println("Date Format with dd MMMM yyyy : "+strDate); formatter = new SimpleDateFormat("dd MMMM yyyy zzzz"); strDate = formatter.format(date); System.out.println("Date Format with dd MMMM yyyy zzzz : "+strDate); formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z"); strDate = formatter.format(date); System.out.println("Date Format with E, dd MMM yyyy HH:mm:ss z : "+strDate); } }
Output:
Date Format with MM/dd/yyyy : 04/13/2015 Date Format with dd-M-yyyy hh:mm:ss : 13-4-2015 10:59:26 Date Format with dd MMMM yyyy : 13 April 2015 Date Format with dd MMMM yyyy zzzz : 13 April 2015 India Standard Time Date Format with E, dd MMM yyyy HH:mm:ss z : Mon, 13 Apr 2015 22:59:26 IST