我正在使用 google data api,它以日期时间格式给出日期。我想将此 DateTime 格式日期转换为公历日期格式。有谁知道这样做的任何方法?
**编辑问题*
我正在使用 google data api,它以日期时间格式给出日期。我想将此 DateTime 格式日期转换为公历日期格式。有谁知道这样做的任何方法?
**编辑问题*
该课程绝对不是 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 来处理这个问题可能的。
还要检查这个
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;
}
这是我为我的一个应用程序创建的学习测试:
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
使用时区偏移量。
这很简单:
DateTime d = event.getEdited();
Date d = new Date(d.getValue());