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

3
我正在使用谷歌数据API,该API以日期时间格式提供日期。我想将此DateTime格式的日期转换为公历日期格式。是否有人知道如何做到这一点的方法?
4个回答

2

那个绝对不是谷歌最好的工作(我链接到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来处理。


有趣。我会试一下。谢谢! - yogsma

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

0

这很简单:

DateTime d = event.getEdited();

Date d = new Date(d.getValue());

0

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

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 的方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接