Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Converting a datetime into gregorian calender time(date)

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

**Edited the question*

like image 325
yogsma Avatar asked Feb 27 '26 16:02

yogsma


2 Answers

That class is definitely not Google's best work (I link to the API so I can confirm we are talking about the same class and version). Here is what I would 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();

From there you can make a Calendar. Dealing with what to do if the DateTime represents a Date only, and timezone issues, you can deal with from there (I don't know your requirements) but if you have to get serious about it, use JodaTime to handle this as much as possible.

like image 57
Yishai Avatar answered Mar 01 '26 20:03

Yishai


Also check this

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;
    }
like image 42
NoNaMe Avatar answered Mar 01 '26 21:03

NoNaMe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!