Question

I want to ask, how can I put Date from Parse.com (which is in format "Sat Mar 01 15:52:20 GMT+1:00 2014") to same Date type as the date and time in system. Second thing I want to ask is, how to get only for example "month:day:hour:minute:year" from Parse.com even though the Date is given in format as written above.

Thanks for any suggestion!

Was it helpful?

Solution

You just try like this you can achieve the Date format conversion easily...

    String inputPattern = "yyyy-MM-dd hh:mm:ss";
    String outputPattern = "dd-MMM-yyyy   hh:mm a";
    SimpleDateFormat inFormat = new SimpleDateFormat(inputPattern);
    SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);

    Date date1 = null;

    if (date != null) {
        try {
            date1 = inFormat.parse(date);
            str = outputFormat.format(date1);
            Log.d(null, str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    } else {
        str = " ";
    }

OTHER TIPS

tl;dr

OffsetDateTime.parse(  // Parse input string as a `OffsetDateTime` object, with the given offset-from-UTC.
    "Sat Mar 01 15:52:20 GMT+1:00 2014" , 
    DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss O uuuu" , Locale.US )    
).toInstant()          // Convert from the given offset-from-UTC to UTC.

Details

You may be confused about how date and time tracking work in Java. The java.util.Date class tracks the number of milliseconds since the Unix epoch. It has no String inside.

java.time

The modern approach uses the java.time classes.

If you are receiving a string such as "Sat Mar 01 15:52:20 GMT+1:00 2014", parse as a OffsetDateTime. By the way, this is a terrible format. Better to exchange date-time values as text using standard ISO 8601 formats, whenever possible.

Specify a formatting pattern to match your input.

Note the Locale argument. Locale to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

String input = "Sat Mar 01 15:52:20 GMT+1:00 2014" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss O uuuu" , Locale.US );
OffsetDateTime odt = OffsetDateTime.parse( input , f  );

Generate a String to represent the value of this OffsetDateTime object in standard ISO 8601 format.

odt.toString(): 2014-03-01T15:52:20+01:00

To generate strings in other formats, search Stack Overflow for DateTimeFormatter.

To view that same moment through the wall-clock time of UTC, extract a Instant object.

Instant instant = odt.toInstant() ;  // Extract an `Instant`, always in UTC.

Joda-Time

UPDATE: This section is now outmoded, but retained as history. FYI, the Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.

Joda-Time is the go-to library for data-time handling. The java.util.Date & .Calendar & SimpleTextFormat classes bundled with Java are notoriously troublesome (not Sun’s best work). Those old classes have been supplanted in Java 8 by the new java.time package which is inspired by Joda-Time.

In Joda-Time, a DateTime object is similar to a java.util.Date object in that it tracks number of milliseconds-since-Unix-epoch. But different in that a DateTime does know its own assigned time zone.

Here's a bit of code to get you started. Search StackOverflow to find more examples.

One problem with that string you gave as example. The offset-from-GMT is +1:00 without a leading zero before the 1. That cannot be parsed directly by Joda-Time. Hopefully that was a typo made by you and not a lousy format being generated by Parse.com.

The formats you see below are the standard, ISO 8601.

String input = "Sat Mar 01 15:52:20 GMT+01:00 2014";

DateTimeFormatter formatterInput = DateTimeFormat.forPattern( "EEE MMM dd HH:mm:ss 'GMT'Z yyyy" ).withLocale( Locale.ENGLISH );
DateTimeZone timeZone = DateTimeZone.forID( "America/Los_Angeles" );
DateTime dateTimeLosAngeles = formatterInput.withZone( timeZone ).parseDateTime( input );

DateTime dateTimeUtc = dateTimeLosAngeles.withZone( DateTimeZone.UTC );

DateTimeFormatter formatterOutput = DateTimeFormat.forStyle( "SS" );
String output = formatterOutput.print( dateTimeLosAngeles );

Dump to console…

System.out.println( "input: " + input );
System.out.println( "dateTimeLosAngeles: " + dateTimeLosAngeles );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "output: " + output );

When run…

input: Sat Mar 01 15:52:20 GMT+01:00 2014
dateTimeLosAngeles: 2014-03-01T06:52:20.000-08:00
dateTimeUtc: 2014-03-01T14:52:20.000Z
output: 3/1/14 6:52 AM

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top