Pergunta

I use the following simple date format to parse a string,

SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
String time = "Wed Mar 06 21:00:00 IST 2014";
Date date = dateFormat.parse(time);

This throws no error whreas,

SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
String time = "Wed Mar 06 21:00:00 KST 2014";
Date date = dateFormat.parse(time);

Throws unparseable date exception.

where, IST - Indian standard time *KST - korean standard time*

I have decided to remove time zone because of the exception.

Is there any other way to work around this issue?

Please help

Foi útil?

Solução 2

Both the dates are invalid, hence both would throw the error:

String time = "Wed Mar Thu 21:00:00 IST 2014";

Thu is invalid as the day of the week is already consumed by EEE - Wed combination. You need to set a day of the month there.

Try parsing the following date, it should work for your case:

String time = "Wed Mar 12 21:00:00 KST 2014";

Outras dicas

Avoid Time Zone Codes

Avoid those 3 or 4 letter codes for time zones. They are neither standardized nor unique. For example, the IST you cite as "Indian Standard Time" is also "Irish Standard Time". Plus they are confusing when in fact you may mean "Summer Time"/"Daylight Saving Time". Instead use proper time zone names, usually made up of a country plus primary city.

Use Sensible Formats For Date-Time Strings

That format "Wed Mar Thu 21:00:00 IST 2014" is not good. Besides being wrong (looks like you typed "Wed" or "Thu" where you should have a day-of-month number), it uses the 3-4 letter code, contains superfluous data (day of week), and assumes English readers. When moving date-time values around as text, use the sensible ISO 8601 format: YYYY-MM-DDTHH:MM:SS.sssZ such as 2014-03-04T23:20:28Z.

If changing that format is out of your control, you'll have to determine all the possible 3-4 letter codes that may be used in your data sets and see if your date-time library can handle them.

Avoid java.util.Date/Calendar

The java.util.Date and .Calendar classes are notoriously troublesome. They are outmoded as of Java 8 with the new java.time package. That package is based on the Joda-Time library. Use either Joda-Time or java.time.

Joda-Time

Note that in contrast to java.util.Date, a Joda-Time DateTime object truly knows its assigned time zone.

Here's some Joda-Time 2.3 example code.

String inputIso = "2014-03-19T21:00:00+05:30";
DateTimeZone timeZoneIndia = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeIndia = new DateTime( inputIso, timeZoneIndia );

// Adjust to Korea Time.
DateTimeZone timeZoneKorea = DateTimeZone.forID( "Asia/Seoul" );
DateTime dateTimeKorea = dateTimeIndia.withZone( timeZoneKorea );

// Adjust to UTC (GMT).
DateTime dateTimeUtc = dateTimeKorea.withZone( DateTimeZone.UTC );

String inputLame = "Thu Mar 20 21:00:00 KST 2014";
DateTimeZone timeZone = null;
if( inputLame.contains( "IST" )) { // Assume IST = India time.
    timeZone = DateTimeZone.forID(  "Asia/India" );
    inputLame = inputLame.replace( " IST ", " ");
}
if( inputLame.contains( "KST" )) { // Assume KST = Korea time.
    timeZone = DateTimeZone.forID(  "Asia/Seoul" );
    inputLame = inputLame.replace( " KST ", " ");
}
DateTimeFormatter formatter = DateTimeFormat.forPattern( "EEE MMM dd HH:mm:ss yyyy" ).withZone( timeZone ).withLocale( java.util.Locale.ENGLISH );
DateTime dateTimeLame = formatter.parseDateTime( inputLame );

Dump to console…

System.out.println( "dateTimeIndia: " + dateTimeIndia );
System.out.println( "dateTimeKorea: " + dateTimeKorea );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "dateTimeLame: " + dateTimeLame );

When run…

dateTimeIndia: 2014-03-19T21:00:00.000+05:30
dateTimeKorea: 2014-03-20T00:30:00.000+09:00
dateTimeUtc: 2014-03-19T15:30:00.000Z
dateTimeLame: 2014-03-20T21:00:00.000+09:00

Try this piece of code and see if KST is a valid time zone id

    String[] timezoneIdArr = TimeZone.getAvailableIDs();
    for (String tzId : timezoneIdArr) {
        System.out.println(tzId);
    }

If not, then enter zone id like "Asia/Seoul" or something. That should work.

Please note that I have not tried it. Please check exact spellings of the time zones.

Update:

Try the code below. See what KST yields. Uncomment and try with Asia/Seoul. For your reference, uncomment and see how PST works.

private static void calTest() {
    String zone = "KST";
    //String zone = "Asia/Seoul";
    //String zone = "PST";

    long millis = System.currentTimeMillis();

    //get calendar of Korea time zone.
    Calendar kst = Calendar.getInstance(TimeZone.getTimeZone(zone));

    //set its time to a UTC millisecond value. probably redundant, but just to demonstrate
    kst.setTimeInMillis(millis);

    String formattedKst = formatTime(kst);
    System.out.println(" Original - " + formattedKst);

    //now we convert the formatted string back to a Calendar . 
    Calendar parsedKst = parseTime(formattedKst, zone);
    System.out.println(" Parsed - ");
    System.out.println("" + parsedKst.get(Calendar.YEAR) + "-"
             + (parsedKst.get(Calendar.MONTH) + 1) + "-"
             + parsedKst.get(Calendar.DATE) + " "
             + parsedKst.get(Calendar.HOUR_OF_DAY) + ":"
             + parsedKst.get(Calendar.MINUTE) + ":"
             + parsedKst.get(Calendar.SECOND) + "."
             + parsedKst.get(Calendar.MILLISECOND) + " "
             + parsedKst.getTimeZone().getID() + " "
             + parsedKst.getTimeZone().getDisplayName() + " "
            );




}

private static Calendar parseTime(String formattedDateTime, String ID) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX zzz");
    sdf.setTimeZone(TimeZone.getTimeZone(ID));
    sdf.setLenient(false);
    try {
        sdf.parse(formattedDateTime);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return sdf.getCalendar();
}

private static String formatTime(Calendar cal) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX zzz");
    sdf.setCalendar(cal);
    return sdf.format(cal.getTime());
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top