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);

 

CEO & Co-Founder at SolGuruz® | Organiser @ GDG Ahmedabad | Top 0.1% over StackOverflow | 15+ years experienced Tech Consultant | Helping startups with Custom Software Development

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