问题
I have to compute something based on the Calendar's date, but I am receiving the complete Gregorian Calendar's String value.
Eg i/p received {may be - "new GregorianCalendar().toString()"} as String :- java.util.GregorianCalendar[time=1410521241348,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/London",offset=0,dstSavings=3600000,useDaylight=true,transitions=242,lastRule=java.util.SimpleTimeZone[id=Europe/London,offset=0,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=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=8,WEEK_OF_YEAR=37,WEEK_OF_MONTH=2,DAY_OF_MONTH=12,DAY_OF_YEAR=255,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=0,HOUR_OF_DAY=12,MINUTE=27,SECOND=21,MILLISECOND=348,ZONE_OFFSET=0,DST_OFFSET=3600000]
I want to extract the Calendar's date value to process further computation.
回答1:
Other answers are too complicated or wrong. The following will give you the milliseconds since the epoch, which is a universal timestamp that you can easily convert to most time representation classes, including Calendar
or Date
:
Pattern gregorianPattern = Pattern.compile("^java.util.GregorianCalendar\\[time=(\\d+).*");
Matcher matcher = gregorianPattern.matcher(param);
if(matcher.matches()) {
return Long.parseLong(matcher.group(1));
}
回答2:
You could find the time in the input string and convert it to a Gregorian Calendar. Then you would have to set its timezone as specified in the ZoneInfo field. Something like this might work:
String calendarAsString="java.util.GregorianCalendar[time=1410521241348,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id=\"Europe/London\",offset=0,dstSavings=3600000,useDaylight=true,transitions=242,lastRule=java.util.SimpleTimeZone[id=Europe/London,offset=0,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=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=8,WEEK_OF_YEAR=37,WEEK_OF_MONTH=2,DAY_OF_MONTH=12,DAY_OF_YEAR=255,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=0,HOUR_OF_DAY=12,MINUTE=27,SECOND=21,MILLISECOND=348,ZONE_OFFSET=0,DST_OFFSET=3600000]";
int timeStart=calendarAsString.indexOf("time=")+5;
int timeEnd=calendarAsString.indexOf(',');
String timeStr=calendarAsString.substring(timeStart, timeEnd);
long timeInMillis=Long.parseLong(timeStr);
int timezoneIdStart=calendarAsString.indexOf("\"")+1;
int timezoneIdEnd=calendarAsString.indexOf("\",");
String timeZoneStr=calendarAsString.substring(timezoneIdStart, timezoneIdEnd);
System.out.println("time="+timeInMillis+" zone="+timeZoneStr);
Calendar calendar=Calendar.getInstance(TimeZone.getTimeZone(timeZoneStr));
calendar.setTimeInMillis(timeInMillis);
System.out.println(calendarAsString);
System.out.println(calendar);
or you can use a regular expression to do it, instead
String regex="time=([0-9]*),.*ZoneInfo\\[id=\"([^\"]*)\"";
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(calendarAsString);
matcher.find();
timeStr=matcher.group(1);
timeInMillis=Long.parseLong(timeStr);
timeZoneStr=matcher.group(2);
System.out.println("time="+timeInMillis+" zone="+timeZoneStr);
calendar=Calendar.getInstance(TimeZone.getTimeZone(timeZoneStr));
calendar.setTimeInMillis(timeInMillis);
System.out.println(calendar);
Note: if you just want the calendar's Date value, you can construct it from the timeInMillis, without having to reconstruct the whole GregorianCalendar object (and without having to find the timezone if you don't want to).
Date date=new Date(timeInMillis);
回答3:
GregorianCalendar g=new GregorianCalendar(1975, 5, 7);
Date d=g.getTime();
System.out.println(g.toString());
SimpleDateFormat formatter=new SimpleDateFormat("yyyy MM dd");
System.out.println(formatter.format(d));
This is way to grab date from GregorianCalendar. i wish this will help to you
Adding more to this, you can retrieve any information you want using the format. It's just a matter of providing the correct format.
Ex:
Adding z or Z provides you with the timezone information
"yyyy MM dd z" - 2014 10 12 PDT
"yyyy MM dd Z" - 2014 10 12 -0700
Adding a 'T' would result in something like this:
"yyyy-MM-dd'T'HH:mm:ss.sssZ" - 2014-10-12T14:23:51.890+0530
In this HH represents hours in 24 hour format mm minutes ss seconds sss milliseconds.
来源:https://stackoverflow.com/questions/25807422/how-to-convert-gregorian-string-to-gregorian-calendar