2010-03-22 238 views

回答

2

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來處理此問題可能。

+0

有意思。我會試試這個。謝謝!! – yogsma 2010-03-22 21:21:36

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也使用時區偏移量。

相關問題