Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display Java.util.Date in a specific format

Tags:

java

date

I have the following scenario :

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); System.out.println(dateFormat.parse("31/05/2011")); 

gives an output

Tue May 31 00:00:00 SGT 2011 

but I want the output to be

31/05/2011  

I need to use parse here because the dates need to be sorted as Dates and not as String.

Any ideas ??

like image 672
bhavya Avatar asked Jun 07 '11 08:06

bhavya


People also ask

How do I change the date format in Java Util?

// Setting the pattern SimpleDateFormat sm = new SimpleDateFormat("mm-dd-yyyy"); // myDate is the java. util. Date in yyyy-mm-dd format // Converting it into String using formatter String strDate = sm. format(myDate); //Converting the String back to java.

How do I print a date in a specific format?

Formatting Dates String pattern = "yyyy-MM-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat. format(new Date()); System. out. println(date);

How do you format a date in Java?

Java SimpleDateFormat with Locale String pattern = "EEEEE MMMMM yyyy HH:mm:ss. SSSZ"; SimpleDateFormat simpleDateFormat =new SimpleDateFormat(pattern, new Locale("fr", "FR")); String date = simpleDateFormat. format(new Date()); System.


1 Answers

How about:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); System.out.println(dateFormat.format(dateFormat.parse("31/05/2011")));  > 31/05/2011 
like image 53
Luciano Fiandesio Avatar answered Sep 22 '22 20:09

Luciano Fiandesio