Convert calendar String to Calendar Object in java
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
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.
In your code, you have two separate GregorianCalendar
objects - one called calendar
and one called gc
. You're setting one calendar object to the date that you want, then printing out the other one.
感谢@Ole,我终于找到了解决方案,它的工作原理。
Calendar calendar = Calendar.getInstance();;
calendar.setTime(new Date("2017/02/02"));
String[] ds = calendar.toString().split("=");
String[] ds2 = ds[1].split(",");
try {
Calendar cal = Calendar.getInstance();;
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
cal.setTimeInMillis(Long.valueOf(ds2[0]));
System.out.println(formatter.format(cal.getTime()));
}
catch(Exception e) {
}
链接地址: http://www.djcxy.com/p/36646.html
上一篇: 如何减去两个约会日期?
下一篇: 在java中将日历字符串转换为日历对象