【问题标题】:Java - Converting a datetime into gregorian calender time(date)Java - 将日期时间转换为公历时间(日期)
【发布时间】:2010-03-22 21:04:25
【问题描述】:

我正在使用 google data api,它以日期时间格式给出日期。我想将此 DateTime 格式日期转换为公历日期格式。有谁知道这样做的任何方法?

**编辑问题*

【问题讨论】:

    标签: java datetime google-data-api


    【解决方案1】:

    class 绝对不是 Google 最好的作品(我链接到 API,所以我可以确认我们谈论的是相同的类和版本)。这是我会做的:

       public class DateTimeConverter extends DateTime {
             private DateTime originalTime;
    
             public DateTimeConverter(DateTime originalTime) {
                  this.originalTime = originalTime;
             }
    
             public java.util.Date getDate() {
                  return new java.util.Date(this.value);
             }
       }
    

    示例用法:

         DateTime originalTime;
         //Populate variable and then:
         java.util.Date myDate = new DateTimeConverter(originalTime).getDate();
    

    您可以从那里制作日历。处理如果 DateTime 仅代表日期和时区问题,您可以从那里处理(我不知道您的要求)但是如果您必须认真对待它,请使用 JodaTime 来处理这个问题可能。

    【讨论】:

    • 有趣。我会试试这个。谢谢!!
    【解决方案2】:

    也检查一下

    public static XMLGregorianCalendar getGregorianDate(Date date) {
            XMLGregorianCalendar xMLGregorianCalendar = null;
            try {
                GregorianCalendar c = new GregorianCalendar();
                c.setTime(date);
                xMLGregorianCalendar = DatatypeFactory.newInstance()
                        .newXMLGregorianCalendar(c);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return xMLGregorianCalendar;
        }
    
        public static Date getDate(XMLGregorianCalendar xmlGregorianCalendar) {
            Date date = xmlGregorianCalendar.toGregorianCalendar().getTime();
            return date;
        }
    

    【讨论】:

      【解决方案3】:

      很简单:

      DateTime d = event.getEdited();
      
      Date d = new Date(d.getValue());
      

      【讨论】:

        【解决方案4】:

        这是我为我的一个应用程序创建的学习测试:

        package learning;
        
        import java.text.ParseException;
        import java.text.SimpleDateFormat;
        import java.util.Date;
        import java.util.GregorianCalendar;
        import java.util.TimeZone;
        
        import org.apache.commons.lang3.ArrayUtils;
        import org.junit.Assert;
        import org.junit.Test;
        
        import com.google.gdata.data.DateTime;
        
        public class GDataDateTimeTest {
        
            @Test public void should_be_converted_to_gregorian_calendar() {
        //      String[] timeZoneIDs = TimeZone.getAvailableIDs();
        //      for (String timeZoneID : timeZoneIDs)
        //          System.out.println(timeZoneID);
        
                Date startingDate = date("02 Jan 2013");
                String timeZoneID = "Europe/London";
        
                DateTime datetime = new DateTime(startingDate, TimeZone.getTimeZone(timeZoneID));
        
                // *** start conversion to a gregorian calendar:
                // we need to multiply the GData DateTime.tzShift * 60000 (the opposite of what you can find in the constructor used previously [search for the code])
                String[] availableTimeZoneIDsForOffset = TimeZone.getAvailableIDs(datetime.getTzShift() *60000);
                // available timeZone IDs for the offset should contains our timeZoneID
                Assert.assertTrue(ArrayUtils.contains(availableTimeZoneIDsForOffset, timeZoneID));
        
                // then create a GregorianCalendar with one of the timeZone found for the timeZone offset
                // and set time in millis with GData DateTime.value
                String oneOfTheTimeZonesFoundBefore = availableTimeZoneIDsForOffset[0];
                GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone(oneOfTheTimeZonesFoundBefore));
                cal.setTimeInMillis(datetime.getValue());
                Assert.assertEquals(startingDate.getTime(), cal.getTimeInMillis());
            }
        
            private Date date(String dateAsText) {
                try {
                    return new SimpleDateFormat("dd MMM yyyy").parse(dateAsText);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        
        }
        

        我添加了 cmets 来帮助您找到如何使用时区偏移量转换 GData DateTime。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-20
          • 1970-01-01
          • 2014-07-01
          • 1970-01-01
          相关资源
          最近更新 更多