3

私は日時形式で日付を与えるグーグルデータAPIを使用しています。このDateTime形式の日付をグレゴリオ暦の日付形式に変換したいと思います。誰かがこれを行うための方法を知っていますか?

**質問を編集しました*

4

4 に答える 4

2

そのクラスは間違いなく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を使用してこれを処理します。可能。

于 2010-03-22T21:18:42.823 に答える
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;
    }
于 2012-12-10T07:24:38.017 に答える
0

それは非常に簡単です:

DateTime d = event.getEdited();

Date d = new Date(d.getValue());
于 2013-06-30T12:53:31.360 に答える
0

これは、私のアプリケーションの1つに対して作成した学習テストです。

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タイムゾーンオフセットを使用して変換する方法を見つけるのに役立つコメントも追加しました。

于 2013-08-29T20:08:47.520 に答える