【问题标题】:Covert RFC3339 DateTime to Date in java [duplicate]在java中将RFC3339 DateTime转换为日期[重复]
【发布时间】:2014-07-22 12:41:11
【问题描述】:

如何在 java 中将RFC 3339com.google.api.client.util.DateTime 转换为 DateTime。

例如,我收到“2014-07-21T16:35:27.000Z”,我需要将其转换为“2014 年 7 月 15 日下午 6:07:25”格式。

还有什么可以转换的吗?

这是我尝试过的。

我已将 DateandTime 作为字符串保存在 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);
           }        

现在要更新日期,我得到的是 Rfc3339 格式的日期,我必须将其转换为 java.util.Date 格式。

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

}

【问题讨论】:

  • 您想将字符串从一种格式转换为另一种格式,还是将DateTime 实例转换为另一个类的实例?第二个字符串在哪个时区?
  • 我已经编辑了这个问题。请检查一下
  • RFC 3339ISO 8601 标准的配置文件。因此,您可以查看 Stack Overflow 上有关“ISO 8601”的许多帖子。要生成特定格式的字符串,请搜索 DateTimeFormatter 类。

标签: java mongodb date datetime


【解决方案1】:

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

新日期(dt.getValue());

【讨论】:

    【解决方案2】:

    如果我理解你的问题,你可以像这样使用SimpleDateFormat

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

    【讨论】:

      【解决方案3】:

      com.google.api.client.util.DateTime 包含两个字段: - 自纪元以来的毫秒数 tzShift - 相对于 UTC 的最小时间偏移 因此,您必须将时间转移考虑在内。 新日期(dateTime.getValue() + dateTime.getTimeZoneShift() * 60000L)

      【讨论】:

        猜你喜欢
        • 2012-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-19
        • 2019-02-19
        • 2020-09-08
        • 1970-01-01
        • 2018-08-09
        相关资源
        最近更新 更多