简体   繁体   English

时区的日历问题

[英]Calender issue with time zone

I'm having a weird situation with Java Calendar. 我在使用Java日历时遇到了奇怪的情况。 I'm using dozer mapper to map the objects. 我正在使用推土机映射器来映射对象。

I want to write a method that will convert this object to the following format. 我想编写一个将这个对象转换为以下格式的方法。 yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

say element 2010-11-11T09:30:47.000Z 说元素2010-11-11T09:30:47.000Z

public Calender getValue(Date source,Calender c) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.setTime(source);
return calendar;
}

When I run the program, it is printing 2010-11-11T04:00:47.000Z - Because we are setting the Timezone to be GMT, (9.30 - 5.30 = 4.00) 当我运行程序时,它正在打印2010-11-11T04:00:47.000Z-因为我们将时区设置为GMT,(9.30-5.30 = 4.00)

I want my object to have same format and value.if I don't set TimeZone to GMT, it will show as 2008-11-21T09:30:47.000+05:30. 我希望我的对象具有相同的格式和值。如果我未将TimeZone设置为GMT,它将显示为2008-11-21T09:30:47.000 + 05:30。

I want it as 2010-11-11T09:30:47.000Z. 我希望它是2010-11-11T09:30:47.000Z。

I tried added 5.30 to calender. 我尝试将5.30添加到日历中。

    calendar.add(Calendar.HOUR, 5);
    calendar.add(Calendar.MINUTE, 30)

then it works.But if this is ran from any other place, difference won't be 5.30.So I cannot add 5.30 to calenderget 如果它是从其他地方运行的,则差异不会是5.30,所以我无法在calenderget中添加5.30

Is there any way to get rid of this problem? 有什么办法可以摆脱这个问题? I want to return Calender object. 我想返回日历对象。

Any suggestions or help would be much appreciated 任何建议或帮助将不胜感激

Use a pattern. 使用模式。 FE: FE:

String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);

Also, 也,

SimpleDateFormat dateformatyyyyMMdd = new SimpleDateFormat("yyyyMMdd");
String date_to_string = dateformatyyyyMMdd.format(dateNow);

you can use SimpleDateFormat like this. 您可以像这样使用SimpleDateFormat。

   SimpleDateFormat formatter, FORMATTER;
   formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
   String oldDate = "2011-03-10T11:54:30.207Z";
   Date date = formatter.parse(oldDate.substring(0, 24));
   FORMATTER = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS");
   System.out.println("OldDate-->"+oldDate);
   System.out.println("NewDate-->"+FORMATTER.format(date));

Output OldDate-->2011-03-10T11:54:30.207Z NewDate-->10-Mar-2011 11:54:30.207 输出OldDate-> 2011-03-10T11:54:30.207Z NewDate-> 2011年3月10日11:54:30.207

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM