Calendar Class in Java, ParseException

So, I am experimenting with a class Calendarin Java, and I am writing a method that returns a Calendar object.

I want this method to return an object Calendarcontaining "Sun Feb 09 22:49:36 +0000 2014".

Now I'm (debatably) not lazy, and I worked a little on my method.

    Calendar cal = Calendar.getInstance();

    SimpleDateFormat sdf = new SimpleDateFormat(
            "EEE MMM dd HH:mm:ss Z yyyy");
    try {
        cal.setTime(sdf.parse("Sun Feb 09 22:49:36 +0000 2014"));
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return cal;

The problem is that he keeps telling me what I got ParseExceptionthat he is "Unparseable date".

I thought my logic was pretty correct, but I'm starting to doubt it.

I would prefer that it does not import more than Calendar, but is SimpleDateFormatalso very convenient.

The less imported, the better.

Does anyone see how I can achieve?

EDIT 1

Tried to run the code in the main method and just print the result without any difference in Exception.

:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class TestingMyCalendar {

public static void main(String[] args) {

    Calendar cal = Calendar.getInstance();

    SimpleDateFormat sdf = new SimpleDateFormat(
            "EEE MMM dd HH:mm:ss Z yyyy");
    try {
        cal.setTime(sdf.parse("Sun Feb 09 22:49:36 +0000 2014"));
    } catch (ParseException e) {
        e.printStackTrace();
    }

    System.out.println(cal);

}
}

, , main:

java.text.ParseException: Unparseable date: "Sun Feb 09 22:49:36 +0000 2014"
at java.text.DateFormat.parse(Unknown Source)
at TestingMyCalendar.main(TestingMyCalendar.java:15)
java.util.GregorianCalendar[time=1391987659892,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Berlin",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Europe/Berlin,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2014,MONTH=1,WEEK_OF_YEAR=7,WEEK_OF_MONTH=2,DAY_OF_MONTH=10,DAY_OF_YEAR=41,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=14,SECOND=19,MILLISECOND=892,ZONE_OFFSET=3600000,DST_OFFSET=0]
+4
3

Locale.ENGLISH, Sun .

:

   Locale.setDefault(Locale.ENGLISH); 

:

    SimpleDateFormat sdf = new SimpleDateFormat(
        "EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
+4

Europe/Berlin, , Locale. , Try

SimpleDateFormat sdf = 
          new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
+2

. .

, :

, , . , ( , ), .

, , - JVM . , , , .

Joda

, . java.util.Calendar java.util.Date. - , . , , , . Joda-Time. , Java 8, java.time. *, Joda-Time, JSR 310.

, Joda-Time DateTime . java.util.Date, , toString JVM , .

Joda-Time 2.3.

String input = "Sun Feb 09 22:49:36 +0000 2014";

DateTimeZone timeZone = DateTimeZone.forID( "America/Montreal" );
DateTimeFormatter formatter = DateTimeFormat.forPattern( "EEE MMM dd HH:mm:ss Z yyyy" ).withLocale( Locale.ENGLISH ).withZone( timeZone );
DateTime dateTime = formatter.parseDateTime( input );

...

System.out.println( "dateTime: " + dateTime );
System.out.println( "Same dateTime in UTC/GMT: " + dateTime.withZone( DateTimeZone.UTC ) );

...

dateTime: 2014-02-09T17:49:36.000-05:00
Same dateTime in UTC/GMT: 2014-02-09T22:49:36.000Z
+2
source

Source: https://habr.com/ru/post/1526074/


All Articles