Arun Kumar
Arun Kumar

Reputation: 225

How to convert the date format in java

String Resultmasterid=res1.getString(1);
System.out.println(Resultmasterid);
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMddHHMM");

Date varDate1=dateFormat1.parse(Resultmasterid);
dateFormat1=new SimpleDateFormat("dd-MMM-yyyy HH:MM");
String Final_admitDT=dateFormat1.format(varDate1);

This is my code I get the date as yyyyMMddHHMM format, now I need to change the format in dd-mmm-yyyy HH:MM. I get the result but it is not correct. Can any one help me on this please.

Upvotes: 0

Views: 10619

Answers (3)

LaurentG
LaurentG

Reputation: 11717

Your patterns are wrong, you should use MM for month and mm for minutes:

SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMddHHmm");
...
dateFormat1 = new SimpleDateFormat("dd-MMM-yyyy HH:mm");

By the way, you should also respect the Sun code conventions and not use local variables starting with a capital letters (e.g. finalAdmitDT instead of Final_admitDT).

Upvotes: 0

Rahul
Rahul

Reputation: 45060

You need to use

dateFormat1=new SimpleDateFormat("dd-MMM-yyyy HH:mm");

because mm represent the Minute in hour, whereas MM represent Month in year.

Have a look at the docs of SDF for more info on the patterns and pattern letters.

Upvotes: 1

newuser
newuser

Reputation: 8466

SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMddHHmm");    
SimpleDateFormat("dd-MMM-yyyy HH:mm");

instead of

SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMddHHMM");
SimpleDateFormat("dd-MMM-yyyy HH:MM");

SimpleDateFormat

MM indicates month
mm indicates minutes

Upvotes: 1

Related Questions