Java SimpleDateFormat class is used for formatting date and time. In the previous tutorial we have seen the examples of DateFormat class which is also used for the same purpose, the SimpleDateFormat class is a sub class of DateFormat class. In this guide, we will see how to format date and time using this class, along with the examples of methods of this class.
Java SimpleDateFormat Example – Formatting Date and time
In this example, we are formatting the current date and time using SimpleDateFormat class.
import java.text.SimpleDateFormat; import java.util.Date; public class Example { public static void main(String[] args) { //Getting the current date Date date = new Date(); //Specifying the format of the date using SimpleDateFormat SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); //Formatting the date to the specified format String dateString = sdf.format(date); System.out.println("Date in the format of MM-dd-yyyy : "+dateString); sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss"); dateString = sdf.format(date); System.out.println("Date in the format of dd/MM/yyyy hh:mm:ss : "+dateString); sdf = new SimpleDateFormat("dd, MMMM, yyyy"); dateString = sdf.format(date); System.out.println("Date in the format of dd, MMMM, yyyy : "+dateString); //Format with time zone sdf = new SimpleDateFormat("dd, MMMM, yyyy zzzz"); dateString = sdf.format(date); System.out.println("Date in the format of dd, MMMM, yyyy zzzz : "+dateString); //DateFormat day, date, time and time zone sdf = new SimpleDateFormat("E, dd/MMM/yyyy HH:mm:ss z"); dateString = sdf.format(date); System.out.println("Date in the format of E, dd/MMM/yyyy HH:mm:ss z : "+dateString); //DateFormat date and time zone sdf = new SimpleDateFormat("dd MMM yyyy z"); dateString = sdf.format(date); System.out.println("Date in the format of dd MMM yyyy z : "+dateString); } }
Output:
Date in the format of MM-dd-yyyy : 10-19-2017 Date in the format of dd/MM/yyyy hh:mm:ss : 19/10/2017 11:00:47 Date in the format of dd, MMMM, yyyy : 19, October, 2017 Date in the format of dd, MMMM, yyyy zzzz : 19, October, 2017 India Standard Time Date in the format of E, dd/MMM/yyyy HH:mm:ss z : Thu, 19/Oct/2017 23:00:47 IST Date in the format of dd MMM yyyy z : 19 Oct 2017 IST
SimpleDateFormat Example 2: Converting Date to String
In this example we are converting the current date to String using the format() method of SimpleDateFormat.
import java.text.SimpleDateFormat; import java.util.Date; public class Example { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("dd, MM, yyyy"); //converting date to string using format() method String dateString = sdf.format(date); System.out.println("Date is: "+dateString); } }
Output:
Date is: 19, 10, 2017
Java SimpleDateFormat parse() method example
Using the parse() method of the SimpleDateFormat class to convert a given string to date in Java.
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Example { public static void main(String[] args) { //Specifying the format of the date SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); try { String dateString = "19-06-2017"; //String to date conversion Date date = sdf.parse(dateString); System.out.println(date); } catch (ParseException e) { e.printStackTrace(); } } }
Output:
Mon Jun 19 00:00:00 IST 2017
Leave a Reply