How can I retrieve datetiime from mongodb? By comparing the data with jDateChosser Java

ε祈祈猫儿з 提交于 2021-02-05 12:27:43

问题


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.");
    }
}

回答1:


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

  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

A demo with the suggested format:

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

Output:

2021-01-14T08:13:01Z

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.

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.

Demo:

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

Output:

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


来源:https://stackoverflow.com/questions/65698567/how-can-i-retrieve-datetiime-from-mongodb-by-comparing-the-data-with-jdatechoss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!