如何从mongodb检索datetiime?通过将数据与jDateChosser Java进行比较 [英] How can I retrieve datetiime from mongodb? By comparing the data with jDateChosser Java

查看:69
本文介绍了如何从mongodb检索datetiime?通过将数据与jDateChosser Java进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void showdataTable_btnActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    try {
        DateFormat df = new SimpleDateFormat("YYYY-mm-dd'T'HH:MM:ss'Z'");  //set date format

        String set = df.format(dateChoos1.getDate());           //add value to set

        BasicDBObject whereQuery = new BasicDBObject();
        whereQuery.put("datetimes", set);                       //where date via set(date)

        DBCursor cursor = table.find(whereQuery);
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            String ip_address = (String) obj.get("ip_address");
            String mac_address = (String) obj.get("mac_address");
            Date datetimes = (Date) obj.get("datetimes");
            String url = (String) obj.get("url");
            model.insertRow(model.getRowCount(), new Object[]{datetimes, ip_address, mac_address, url});
        }
    } catch (Exception e) {
        System.out.println("Something went wrong.");
    }
}

推荐答案

您的格式 YYYY-mm-dd'T'HH:MM:ss'Z'不正确.让我们讨论一下这种格式的所有错误.

Your format, YYYY-mm-dd'T'HH:MM:ss'Z' is not correct. Let's discuss everything which is wrong with this format.

  1. 您使用的是 Y 而不是 y :符号 Y 用于周年,而 y 用于 Year .检查在年份与周基之间的差异?以了解更多信息.
  2. 您已使用 mm 表示月份:该月份的正确符号是 M .
  3. 您已经使用 MM 分钟了:分钟的正确符号是 m .
  4. 您已将 Z 括在单引号内:符号 Z 用于时区,而'Z'只是字符文字.可能您想将 +00:00 的时区偏移量设置为 Z ,为此,您实际上应该使用 X .
  1. You have used Y instead of y: The symbol Y is used for Week year while y is used for Year. Check Difference between year-of-era and week-based-year? to learn more about it.
  2. You have used mm for month: The correct symbol for the month is M.
  3. You have used MM for minutes: The correct symbol for the minute is m.
  4. You have enclosed Z within single quotes: The symbol, Z is used for Time zone whereas 'Z' is nothing but a character literal. Probably you want to format the timezone offset of +00:00 as Z and for this, you should in fact use X.

因此,正确的格式如下:

So, the correct format is as follows:

yyyy-MM-dd'T'HH:mm:ssX

具有建议格式的演示:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.ENGLISH);
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        System.out.println(sdf.format(date));
    }
}

输出:

2021-01-14T08:13:01Z

请注意, java.util 的日期时间API及其格式API SimpleDateFormat 已过时且容易出错.建议完全停止使用它们,并切换到现代日期时间API .

Note that the date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API.

  • For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7.
  • If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

使用 Date#toInstant java.util.Date 对象(旧类型)转换为 java.time.Instant (现代类型). Instant 表示时间轴上的瞬时点,对于大多数JSON操作而言应该足够. Instant#toString 返回具有UTC时区偏移量的日期时间字符串,该日期时间字符串符合 ISO-8601标准.

Use Date#toInstant to convert a java.util.Date object (the legacy type) to java.time.Instant (the modern type). Instant represents an instantaneous point on the time-line and should be just enough for most of your JSON operations. The Instant#toString returns the date-time string with UTC timezone offset which is compliant with ISO-8601 standards.

演示:

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        Instant instant = date.toInstant();
        // Print the value of instant#toString
        System.out.println(instant);

        OffsetDateTime odt = instant.atOffset(ZoneOffset.UTC);
        System.out.println(odt);
        // Custom format
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssX", Locale.ENGLISH);
        System.out.println(dtf.format(odt));
    }
}

输出:

2021-01-14T08:28:35.659Z
2021-01-14T08:28:35.659Z
2021-01-14T08:28:35Z

这篇关于如何从mongodb检索datetiime?通过将数据与jDateChosser Java进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆