M.A.Murali M.A.Murali - 1 year ago 140
Java Question

Java get month sort name from date

I need to get month short name and date from the following date format.

Given date format is :

2015-12-01 00:00:00
and my output date format is
Dec, 01
. but my code always return
Jan, 01
. please tell me where I am wrong in my code.

String newdate = "";
String ip = "2015-10-01 00:00:00";
try {
String old_format = "yyyy-mm-dd HH:mm:ss";
String new_format = "MMM, dd";
SimpleDateFormat sdf = new SimpleDateFormat(old_format);
Date d = sdf.parse(ip);
SimpleDateFormat sm = new SimpleDateFormat(new_format);
newdate = sm.format(d);
} catch (ParseException e) {

Answer Source

To get the expected result you should use the following format:

String old_format = "yyyy-MM-dd HH:mm:ss";

MM instead of mm to get the two digit month of the year

Please check the table on http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

M -> Month in year

m -> Minute in hour

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download