Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I wan't to convert a string Calendar Object (calendar.toString()) to calendar object. I tried this solution but it show in console the date of the day '12-05-2017' not '02-02-2017'

String calendar object format:

java.util.GregorianCalendar[time=1485993600000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Africa/Casablanca",offset=0,dstSavings=3600000,useDaylight=true,transitions=102,lastRule=java.util.SimpleTimeZone[id=Africa/Casablanca,offset=0,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=10800000,endTimeMode=0]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2017,MONTH=1,WEEK_OF_YEAR=5,WEEK_OF_MONTH=1,DAY_OF_MONTH=2,DAY_OF_YEAR=33,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]

             Calendar calendar = GregorianCalendar.getInstance();
             calendar.setTime(new Date("2017/02/02"));
             System.out.println("calendar : "+calendar.getTime());
             try {
                 GregorianCalendar gc = new GregorianCalendar();
                 DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
                 System.out.println("calendar : "+calendar.getTime());
                 gc.setTimeZone(TimeZone.getTimeZone(calendar.toString()));
                 System.out.println("tme zone : "+gc.getTimeZone());
                 System.out.println("calendar : "+calendar.getTime());
                 System.out.println("calendar : "+calendar.toString());
                 System.out.println(formatter.format(gc.getTime()));
             }
             catch(Exception e) {
                 //If exception, return server TimeStamp
             }

Any help please

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
126 views
Welcome To Ask or Share your Answers For Others

1 Answer

If that were me, I’d look at all the setters of both Calendar and GregorianCalendar and see if I thought I could extract the values needed for the setters from the string. “time=1485993600000” should give you the most important information, the time, and you can feed it into setTimeInMillis(). You ought to be able to get a time zone out of “Africa/Casablanca”. And so forth. You can probably use regular expressions for extracting the fields from the string.

You’d probably have to live with not covering all cases. Your particular GregorianCalendar seems to contain a sun.util.calendar.ZoneInfo and a java.util.SimpleTimeZone; I don’t know whether that is always the case nor what other possibilities there are.

The strict test of your attempt is easy: just call toString() again on your newly constructed instance and see if you get the same string. The difficulty comes if you accept some differences and you need to determine whether the actual differences lie within what you have decided to accept.

Or really, I wouldn’t want to bother if I could avoid it. I’d see if I could find an easier task or an easier way to obtain what you are really trying to obtain. As I already said in a comment, one may use java.time.LocalDate instead of GregorianCalendar. LocalDate.parse() will readily parse the string from LocalDate.toString(), and the problem is solved. Just to give one example of another way to look at it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...