Android – Get current date and time in different format

By -



Related Article: Get current date and time. But this article guides you to fetch current date and time in different formats. Just define your SimpleDateFormat object with desired format and format the current date and time by using format() method.

Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());

SimpleDateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate1 = df1.format(c.getTime());

SimpleDateFormat df2 = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate2 = df2.format(c.getTime());

SimpleDateFormat df3 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss a");
String formattedDate3 = df3.format(c.getTime());

System.out.println("=========> Date 1 => "+formattedDate1);
System.out.println("=========> Date 2 => "+formattedDate2);
System.out.println("=========> Date 3 => "+formattedDate3);

 

Paresh Mayani

Paresh Mayani is a lead android developer from India. Currently, He is Sr. Software engineer at InfoStretch Solutions Pvt. Ltd. He has been developing android apps since very 1st version i.e. Android 1.5, he says he has seen days and nights of Android history. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

Loading Facebook Comments ...
Loading Disqus Comments ...