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]