-1
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.");
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Kamijou
  • 29
  • 6
  • 2
    Instead of "Something went wrong." you should print a proper error message (e.g. `e.getMessage()` - it may help you solving the problem. – Wernfried Domscheit Jan 13 '21 at 09:14
  • it don't have error, But I think datetime format is not match whit datetime format in mongoDB. Becuz it it don't have error and nothing shows. – Kamijou Jan 13 '21 at 09:31
  • Whether anything shows or not, you should follow the advice offered by @WernfriedDomscheit. If you don't follow (good) advice, the chances of getting help are vastly reduced. BTW - This would fail (or work) just as well from a command line app. Debug and test it there. This has nothing to do with Swing. Tag removed. – Andrew Thompson Jan 13 '21 at 10:06
  • 1
    We don't know your data but bear in mind, `Date` object has resolution of milliseconds. Is the time in MongoDB **exactly** the time in your query. You may also have a look at time zones. – Wernfried Domscheit Jan 13 '21 at 12:06
  • What is the datatype of `datetimes` in MongoDB? `date`? `datetime`? `timestamp` with ot without time zone? – Ole V.V. Jan 15 '21 at 05:43
  • Don’t process your date and time as a string. Keep it in a proper date-time object such as `LocalDate` (for a date) or `Instant` (for a point in time). (Do an effort to find the exactly right type.) Hardcoding `Z` as a literal in your format pattern string is wrong, it’s an offset (of 0) from UTC. I recommend that you use [java.time, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) for your date and time work (not `SimpleDateFormat` and `Date`). – Ole V.V. Jan 15 '21 at 05:48
  • This should be helpful: [Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2](https://stackoverflow.com/questions/43039614/insert-fetch-java-time-localdate-objects-to-from-an-sql-database-such-as-h2). Or [my answer here](https://stackoverflow.com/a/54907501/5772882). – Ole V.V. Jan 15 '21 at 05:50

1 Answers1

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.

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
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Thank sir, I'll try it – Kamijou Jan 14 '21 at 05:35
  • ok, Format is change but I can't query where "datetime" why? whereQuery.put("datetimes", s); it not work? – Kamijou Jan 14 '21 at 07:58
  • @SuttipongKamwongsa - I do not have expertise on MongoDB and therefore I can only suggest you some links on SO e.g. https://stackoverflow.com/q/6840540/10819573 – Arvind Kumar Avinash Jan 14 '21 at 08:33
  • 1
    @OleV.V. - Thanks for the feedback and support. Yes, that's what I have mentioned in the answer: `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.`. The reason why I also added the custom format part is that the OP hasn't mentioned the fraction of second in the requirement. Apart from this, this part also demonstrates the use of DTF in comparison to SDF. – Arvind Kumar Avinash Jan 15 '21 at 06:33