[英]Java - Converting a datetime into gregorian calender time(date)
我正在使用google data api,它以日期時間格式提供日期。 我想以公歷日期格式轉換此DateTime格式日期。 有誰知道這樣做的任何方法?
**編輯問題*
還檢查一下
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;
}
該課程絕對不是谷歌的最佳作品(我鏈接到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僅代表Date以及時區問題該怎么辦,你可以從那里處理(我不知道你的要求)但是如果你必須認真對待它,那么使用JodaTime來處理這個問題就像可能。
這很簡單:
DateTime d = event.getEdited();
Date d = new Date(d.getValue());
這是我為我的一個應用程序創建的學習測試 :
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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.