代码之家  ›  专栏  ›  技术社区  ›  yogsma

Java-将日期时间转换为GRIGORIN日历时间(日期)

  •  3
  • yogsma  · 技术社区  · 15 年前

    我使用的是以日期时间格式给出日期的谷歌数据API。我要将此日期时间格式的日期转换为公历日期格式。有人知道怎么做吗?

    **编辑了问题*

    4 回复  |  直到 11 年前
        1
  •  2
  •   Yishai    15 年前

    class 绝对不是谷歌的最佳作品(我链接到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
  •  2
  •   NoNaMe    12 年前

    还要检查这个

    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
  •  0
  •   Pavel Jiri Strnad    12 年前

    很简单:

    DateTime d = event.getEdited();
    
    Date d = new Date(d.getValue());
    
        4
  •  0
  •   Sixro    11 年前

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

    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);
            }
        }
    
    }
    

    我添加了一些注释以帮助您找到如何转换 GData DateTime 也使用时区偏移。

    推荐文章