0

Here is my date String "Thu Feb 25 12:58:28 MST 2016" and I parse using this formate "E MMM dd HH:mm:ss Z yyyy".

But I am getting date parsing error.

What should I do?

I am parsing date string using following function

public static String getDate(String targrtDate) {
    String dateStr = targrtDate;
    DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
    Date date = null;
    try {
        date = (Date) formatter.parse(dateStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    System.out.println(date);

    if (date == null) {
        return "";
    }

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    String formatedDate = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
    String formatedTime = cal.get(Calendar.HOUR) + "/" + cal.get(Calendar.MINUTE);

    Log.i("formatedDate", "" + formatedDate + " " + formatedTime);

    return formatedDate;
}
Harsh Patel
  • 657
  • 11
  • 26
  • The code you are using working fine.. Just import Date class from java.util.Date. If you importing date class from import java.sql.Date then you will get Exception. Just Import Date class from java.sql.Date. – Anil Chandra Varma Dandu Mar 09 '16 at 14:28
  • Yes, I import same class which you told me but If I use Thu Feb 25 12:58:28 GMT 2016 then working fine but when I used MST as a timezone it gives parsing error. – Harsh Patel Mar 09 '16 at 14:31
  • What is the solution of MST timezone? – Harsh Patel Mar 09 '16 at 14:31
  • I have just change your code SimpleDateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy"); it is working fine I have tested date "Thu Feb 25 12:58:28 MST 2016" once check this – Anil Chandra Varma Dandu Mar 09 '16 at 14:34
  • 2
    For safety, you should use `new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy", Locale.US);` because your pattern is locale-sensitive, and your default locale might not be English. – Meno Hochschild Mar 09 '16 at 15:21
  • I have resolved my issue with the help of `new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy", Locale.US);` Thank you @MenoHochschild – Harsh Patel Mar 10 '16 at 06:26

2 Answers2

0

I have resolved my issue of date parsing.

If someone use MST timezone and if it gives an parsing error then please add second parameter of SimpleDateFormat() as

new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy", Locale.US);

So my final code will be

public static String getDate(String targrtDate) {
    String dateStr = targrtDate;
    SimpleDateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy", Locale.US);
    Date date = null;
    try {
        date = (Date) formatter.parse(dateStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    System.out.println(date);

    if (date == null) {
        return "";
    }

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    String formatedDate = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
    String formatedTime = cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE);

    Log.i("formatedDate", "" + formatedDate + " " + formatedTime);

    return formatedDate;
}

Special thanks to @MenoHochschild

Harsh Patel
  • 657
  • 11
  • 26
0

tl;dr

ZonedDateTime.parse( 
    "Thu Feb 25 12:58:28 MST 2016" , 
    DateTimeFormatter.ofPattern( "E MMM dd HH:mm:ss z uuuu" , Locale.US ) 
)

2016-02-25T12:58:28-07:00[America/Denver]

java.time

The modern approach uses the java.time classes. For Android, see last bullets below.

String input =  "Thu Feb 25 12:58:28 MST 2016" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "E MMM dd HH:mm:ss z uuuu" , Locale.US ) ;
ZonedDateTime zdt = ZonedDateTime.parse( input , f ) ;

See this code run live at IdeOne.com. (But note that the JVM on that site is hard-wired to Locale.US, and any other is ignored.)

zdt.toString(): 2016-02-25T12:58:28-07:00[America/Denver]

Your input string is in a terrible format. If possible, change to using standard ISO 8601 formats. The standard formats are practical, easy to parse by machine and easy to read by humans across cultures. Conveniently, these formats are used by default in the java.time classes when parsing/generating strings.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as MST or EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId zEdmonton = ZoneId.of( "America/Edmonton" ) ;
ZoneId zDenver = ZoneId.of( "America/Denver" ) ;
ZoneId zMazatlan = ZoneId.of( "America/Mazatlan" ) ;

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?

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