Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format date in Java

I have the following string:

Mon Sep 14 15:24:40 UTC 2009

I need to format it into a string like this:

14/9/2009

How do I do it in Java?

like image 944
Dejell Avatar asked Jan 21 '23 02:01

Dejell


1 Answers

Use SimpleDateFormat (click the javadoc link to see patterns) to parse the string in one pattern to a fullworthy Date and use another one to format the parsed Date to a string in another pattern.

String string1 = "Mon Sep 14 15:24:40 UTC 2009";
Date date = new SimpleDateFormat("EEE MMM d HH:mm:ss Z yyyy").parse(string1);
String string2 = new SimpleDateFormat("d/M/yyyy").format(date);
System.out.println(string2); // 14/9/2009
like image 63
BalusC Avatar answered Feb 07 '23 11:02

BalusC