-3

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.

3 Answers3

1

You probably have issues with the time zone. The input string September, 25 2012 02:09:42 +0000 is a timestamp in UTC (offset +0000). When you format your date in the desired format, you're not specifying a time zone, so the SimpleDateFormat object 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 SimpleDateFormat object that you use to format the date. For example:

DateFormat df1 = new SimpleDateFormat("MMMM, dd yyyy HH:mm:ss Z");
Date date = df1.parse(PUNCH_TIME);

DateFormat df2 = new SimpleDateFormat("HH:mm a 'on' dd/MM/yyyy");
df2.setTimeZone(TimeZone.getTimeZone("UTC"));
String result = df2.format(date);

System.out.println(result);

Note: You must use dd and not DD for the days; DD means day number of the year, dd means day number in the month (see the API documentation of SimpleDateFormat).

p.s.: Your usage of the words "parse" and "format" is confusing. Parsing means: converting from a string to a Date object, and formatting means the opposite: converting from a Date object to a string.

Jesper
  • 202,709
  • 46
  • 318
  • 350
0

First off you have a double declaration of sdf2. Then, you need to use hh and mm for hours and minutes. Read the documentation: SimpleDateFormat Like in this example:

    SimpleDateFormat sdf1 = new SimpleDateFormat("MMMM, DD yyyy HH:mm:ss Z");

    //September, 25 2012 02:09:42 +0000
    Date date = null;
    try {
        date = sdf1.parse(String.valueOf(PUNCH_TIME));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

    SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm a 'on' DD/MM/yyyy");

    String timeformat=sdf2.format(date);
slybloty
  • 6,346
  • 6
  • 49
  • 70
  • @SmitPatel remember that months are 0-based. So 0 = January, 1 = February, ..., 8 = September, 9 = October, ..., 11 = December. If the month is September, expect 8, not 9. – Jesper Sep 25 '12 at 13:18
  • @SmitPatel with the above code I get this output: `02:09 AM on 25/09/2012`. Maybe your `PUNCH_TIME` is not the correct format. – slybloty Sep 25 '12 at 13:33
0

tl;dr

OffsetDateTime.parse(
    "September, 25 2012 02:09:42 +0000" , 
    DateTimeFormatter.ofPattern( "MMMM, d uuuu HH:mm:ss Z" , Locale.US ) 
).format(
    DateTimeFormatter.ofPattern( "hh:mm a 'on' dd/MM/uuuu" , Locale.US )
)

02:09 AM on 25/09/2012

Avoid legacy date-time classes

The other Answers are now outmoded, using troublesome old date-time classes that are now legacy. The old classes are supplanted by the java.time classes. For earlier Android, see the last bullets below.

java.time

Define a formatting pattern to match your input string.

String input = "September, 25 2012 02:09:42 +0000" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMMM, d uuuu HH:mm:ss Z" , Locale.US ) ;  // Specify a locale for the human language by which to parse the name of the month.

Parse as an OffsetDateTime given that your input specifies an offset-from-UTC but not a full time zone.

OffsetDateTime odt = OffsetDateTime odt = OffsetDateTime.parse( input , f  );

odt.toString(): 2012-09-25T02:09:42Z

To generate a string in an alternate format, define a DateTimeFormatter with a custom formatting pattern. Pay attention to the uppercase/lowercase of your formatting code characters, and study closely the documentation. Note that colons, spaces, and slashes are known by the formatter, so no need to escape those characters with the single-quote marks.

DateTimeFormatter fOutput = DateTimeFormatter.ofPattern( "hh:mm a 'on' dd/MM/uuuu" , Locale.US ) ;
String output = odt.format( fOutput) ;

02:09 AM on 25/09/2012


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154