Java - converting date and time to gregorian calender time (date)

I am using google data api which gives date in datetime format. I want to convert a DateTime date to a grid date format for a calendar. Does anyone know of any methods for this?

** Edited the question *

+3
source share
4 answers

That the class is definitely not the best work of Google (I refer to the API, so I can confirm that we are talking about the same class and version). Here is what I will do:

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

Sample Usage:

     DateTime originalTime;
     //Populate variable and then:
     java.util.Date myDate = new DateTimeConverter(originalTime).getDate();

. , , DateTime , ( ), , JodaTime, , .

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

It is pretty simple:

DateTime d = event.getEdited();

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

This is a tutorial test that I created for one of my applications:

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

}

I added comments to help you find how to convert GData DateTimeusing timezone offset.

0
source

Source: https://habr.com/ru/post/1738097/


All Articles