2

How to covert the RFC 3339 com.google.api.client.util.DateTime to DateTime in java.

For example I am getting "2014-07-21T16:35:27.000Z" which I need to covert into "Jul 15, 2014 6:07:25 PM" format.

There is anyway to convert this?

This is what i tried.

I have saved the DateandTime as a string in mongo db.

        public Map<String, String> getYouTubeLastFetchDateTime(String key) {
            System.out.println("Inserting data first time....");
            Date nowDate = new Date();
            
            Date dateBefore = new Date(nowDate.getTime() - 7 * 24 * 3600
                    * 1000);
            

            Utils utils = new Utils();
            DBCollection collection = utils.getStaging().getCollection(
                    DB_YOUTUBE_COLLECTION_NAME);
        
            if (null != collection) {
                CIPKeyWord keyword = new CIPKeyWord();
                keyword.setLastFeachedTime(dateBefore);
                keyword.setName(key);
                DBObject dbObject = getDBObject(keyword);
                
                collection.save(dbObject);
                // ObjectId id = (ObjectId) dbObject.get("_id");

            }
            queryObject = new BasicDBObject().append("name", key);
            result = dao.findOne(queryObject, DB_YOUTUBE_COLLECTION_NAME);
            lastFetchedTime = (String) result.get("lastFeachedTime");
            nextPageToken = (String) result.get("nextPageToken");
            prevPageToken = (String) result.get("prevPageToken");
            

}

           private String getKeyword() {
             Map<String, String> paginationInfo = utils
            .getYouTubeLastFetchDateTime(key);
             String dateTime = paginationInfo.get("lastFechedDate");
            date = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH)
                .parse(dateTime);

             com.google.api.client.util.DateTime.DateTime lastFechedDate = new DateTime(date);
           }        

Now to update the date I am getting the date in Rfc3339 format which i have to convert to java.util.Date format.

     public static boolean updateYouTubeLastFetchDate(String keyword,
        DateTime newFetchTime, String nextPageToken, String prevPageToken){

      BasicDBObject updateDocument = new BasicDBObject();
            updateDocument.append(
                    "$set",
                    new BasicDBObject().append("nextPageToken",
                            nextPageToken).append("prevPageToken",
                            prevPageToken)
                                         .append("lastFeachedTime",
                                          newFetchTime.toString())
                                         );
            
            CIPDBUtils utils = new CIPDBUtils();
            DBCollection collection = utils.getStaging().getCollection(
                    DB_YOUTUBE_COLLECTION_NAME);

            collection.update(queryObject, updateDocument);

}

Community
  • 1
  • 1
bharathi
  • 6,019
  • 23
  • 90
  • 152
  • 1
    Do you want to convert a string from one format to another, or do you want to convert a `DateTime` instance to an instance of another class? In what timezone is the second string? – Mike Samuel Jul 22 '14 at 12:42
  • I have edit the question. Please check it – bharathi Jul 22 '14 at 12:58
  • [RFC 3339](https://tools.ietf.org/html/rfc3339) is a profile of the [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) standard. So you can look at the many posts on Stack Overflow for "ISO 8601". And for generating a String in a particular format, search for the `DateTimeFormatter` class. – Basil Bourque Oct 17 '16 at 05:27
  • More closely a duplicate of [Java / convert ISO-8601 (2010-12-16T13:33:50.513852Z) to Date object](http://stackoverflow.com/q/4525850/642706) – Basil Bourque Feb 14 '17 at 03:32

3 Answers3

4

com.google.api.client.util.DateTime dt = ...;

new Date(dt.getValue());

3

If I understand your question, you can use SimpleDateFormat like so,

String in = "2014-07-21T16:35:27.000Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
System.out.println(sdf.parse(in));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

com.google.api.client.util.DateTime holds two fields: value - ms since epoch tzShift - Min of time shift with respect to UTC So, you have to take the time shift into account. new Date(dateTime.getValue() + dateTime.getTimeZoneShift() * 60000L)

Uri
  • 29
  • 3