I want to parse a date into my format like 02:09 AM 25/09/2012 but I can’t. I used this code.
SimpleDateFormat sdf1 = new SimpleDateFormat("MMMM, DD yyyy HH:mm:ss Z");
//September, 25 2012 02:09:42 +0000
Date date = sdf1.parse(String.valueOf(PUNCH_TIME));
SimpleDateFormat sdf2 = new SimpleDateFormat("HH':'mm a 'on' DD'/'MMMM'/'yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("MM");
String timeformat=sdf2.format(date);
txtHomePunchStatus.setText("You have Punched In at "+timeformat);
and I got You have punched IN at 7:52 AM on 25/01/2012.
You probably have issues with the time zone. The input string
September, 25 2012 02:09:42 +0000is a timestamp in UTC (offset+0000). When you format your date in the desired format, you’re not specifying a time zone, so theSimpleDateFormatobject is going to show your date in your local time zone, which is probably not UTC.What you can do is set the time zone on the
SimpleDateFormatobject that you use to format the date. For example:Note: You must use
ddand notDDfor the days;DDmeans day number of the year,ddmeans day number in the month (see the API documentation ofSimpleDateFormat).p.s.: Your usage of the words “parse” and “format” is confusing. Parsing means: converting from a string to a
Dateobject, and formatting means the opposite: converting from aDateobject to a string.