【问题标题】:How to read date (Timestamp) from MongoDB using Java如何使用 Java 从 MongoDB 读取日期(时间戳)
【发布时间】:2023-03-30 12:26:01
【问题描述】:

我正在尝试以以下格式从 MongoDB 读取日期字段

Formate: YYYY-MM-dd HH:mm:ss.SSSSSS

2017-01-23-10.46.07.812000 - DB2
2017-01-23T16:46:07.812Z   - Stored in MongoDB (While viewing from GUI tool)
Mon Jan 23 22:16:07 IST 2017 - Result/Reading from MongoDB

// Formatter for the input date
final DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");
final ZonedDateTime dateFiledParsed = ZonedDateTime.parse(dateFiled.toString(), inputFormat);
final DateTimeFormatter outputFormat3 = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss.SSSSSS");
System.out.println(outputFormat3.format(publicationDateParsed));

Result: 2017-01-23 22:16:07.000000

结果 2017-01-23 22:16:07.000000,而不是 000 应该是 812(原始值:2017-01-23-10.46.07.812000)

注意:使用 MongoDB Java 驱动程序 3.4。

提前谢谢你!

婆罗洲

【问题讨论】:

  • 如果它实际上是一个 BSON 日期(这意味着它在 mongo shell 中查看时显示为 ISODate),那么解析为 java.util.Date 的任何内容都是查询的预期输入格式。如果您发送任何其他格式,那么它将不匹配。另请注意,除非您了解转换过程,否则请使用 UTC 值而不是本地时间。
  • 什么是dateFiled或dateFiled.toString()的字符串输出?
  • @Seelenvirtuose - dateFiled 是“日期 dateFiled = “某个日期”

标签: java mongodb mongodb-java mongodb-java-3.3.0


【解决方案1】:

您可以使用 Java 的 SimpleDateFormat 来相应地格式化日期。例如,假设您使用 the proper ISODate type 在 MongoDB 中插入日期:

> db.test.find()
{
  "_id": ObjectId("597813a12dbe1d773beb11d2"),
  "date": ISODate("2017-01-23T16:46:07.812Z")
}

此代码打印正确的日期:

Document doc = collection.find().first();
Date date = doc.getDate("date");
SimpleDateFormat formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
formattedDate.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(formattedDate.format(date));

输出是:

2017-01-23 16:46:07.812

【讨论】:

  • 非常感谢!它的炒锅符合预期
  • 使用推土机映射器时如何做到这一点
【解决方案2】:

在我的情况下,在将 date 传递给 MongoDb 时,下一个代码工作:

SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(dateFormat.format(date));

检索时:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(dateFormat.format(date));

【讨论】:

    【解决方案3】:

    这 2 个方法将匹配 mongo 的日期格式(java 中的 util.Date)

    public static String convertToString(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        dateFormat.setTimeZone(TimeZone.getTimeZone(zone));
        return dateFormat.format(date);
    }
    
    public static Date convertToDate(String strDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        dateFormat.setTimeZone(TimeZone.getTimeZone(zone));
        Date parsedDate = null;
        try {
            parsedDate = dateFormat.parse(strDate);
        } catch (ParseException e) {
            log.error(e.getMessage());
        }
        return parsedDate;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多