# How can I convert a Gregorian string to a Gregorian calendar?

I need to calculate something based on a calendar date, but I am getting the full Gregorian String value.

For example i / p is 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 date value of a calendar to handle further calculations.

+3

source to share

The other answers are too complicated or wrong. The following will give you milliseconds since epoch, which is a universal timestamp that you can easily convert to most time representation classes, including `Calendar`

either `Date`

:

``````Pattern gregorianPattern = Pattern.compile("^java.util.GregorianCalendar\\[time=(\\d+).*");

Matcher matcher = gregorianPattern.matcher(param);
if(matcher.matches()) {
return Long.parseLong(matcher.group(1));
}
```

```
+1

source

You can find the time in the input string and convert it to the Gregorian calendar. Then you will need to set your timezone as specified in the ZoneInfo field. Perhaps something like this:

``````    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 regex for this, 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 to use a calendar date value, you can create it from timeInMillis without having to reverse engineer the entire GregorianCalendar object (and without having to find the timezone if you don't want to).

``````    Date date=new Date(timeInMillis);
```

```
+3

source

``````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 the way to get the date from GregorianCalendar. I want this to help you

By adding more to this, you can get whatever information you want using the format. It's just a matter of providing the correct format.

Example:
Adding z or Z provides time zone information

``````"yyyy MM dd z" - 2014 10 12 PDT
"yyyy MM dd Z" - 2014 10 12 -0700
```

```

Adding "T" will result in something like this:

``````"yyyy-MM-dd'T'HH:mm:ss.sssZ" - 2014-10-12T14:23:51.890+0530
```

```

This HH features watches in 24-hour mm min format. ss seconds sss milliseconds.

-1

source

All Articles