Java date - 12am is stored as 24?

…衆ロ難τιáo~ 提交于 2019-12-11 01:52:58

问题


So me and my partner have been working on this project for a while now. We work with dates A LOT in this project, and we recently noticed an issue, and we are rather deep in at this point.

We store our times in SQLlite (Android project) as a formatted string, since a lot of the time they are directly bound to listviews and such.

The problem we noticed, which i found kind of odd, is that that SimpleDateTimeFormat object, when used to format to 24h time (its a medical based project, so 24h time is the convention here) 12:00am-12:59am are formatted to 24:00-24:59, instead of 00:00-00:59...

This isn't too much of an issue until we query the database and order the results by the dates, any data that is between 12:00am and 12:59am will show up at the end of the list, but it should show up at the beginning...

Anyone else encountered this problem? or know a way around it? The best thing possible would be a way to store the data as 00:00 not 24:00.

Cheers


回答1:


I strongly suspect you're using the wrong pattern. We've got to guess as you haven't posted any code (hint, hint), but I suspect you're using a pattern such as

kk:mm:ss

instead of

HH:mm:ss

Sample code:

import java.util.*;
import java.text.*;

public class Test {
    public static void main(String[] args) throws Exception {
        SimpleDateFormat broken = new SimpleDateFormat("kk:mm:ss");
        broken.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
        SimpleDateFormat working = new SimpleDateFormat("HH:mm:ss");
        working.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));

        Date epoch = new Date(0);

        System.out.println(broken.format(epoch));
        System.out.println(working.format(epoch));
    }
}

Additionally, as others have pointed out, you shouldn't be storing your values in string format to start with... avoid string conversions wherever you can, as each conversion is a potential pain point.




回答2:


Please read this and this about how SQLite stores dates (or doesn't store dates). SQLite doesn't have a "Date" type, so it is stored as a string. You should store your date as an integer (milliseconds), and then you can use date and time functions to pull them out (from the first link).

From the documentation

1.2 Date and Time Datatype

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS"). REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar. INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC. Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.

I prefer INTEGER / Unix time storage, then use the built in date and time functions to format when pulling from DB.

EDIT: Also, this will take care of sorting. I'm guessing your current "sorting" of the dates in SQLite is string based, which is bad mmmmkay.




回答3:


What is the format string you are passing to your SimpleDateFormat? According to the docs, using 'H' for the hours should get you 0-23, using 'k' should get you 1-24.



来源:https://stackoverflow.com/questions/8171638/java-date-12am-is-stored-as-24

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