Java日期 - 12am存储为24? [英] Java date - 12am is stored as 24?

查看:239
本文介绍了Java日期 - 12am存储为24?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我和我的合作伙伴已经在这个项目上工作了一段时间。

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.

我们将时间存储在SQLlite中(Android(Android))。我们在这个项目中使用日期A LOT,我们最近注意到一个问题,项目)作为格式化的字符串,因为很多时候他们直接绑定到列表视图等。

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.

我们注意到的问题,我发现奇怪,那SimpleDateTimeFormat对象,当用于格式化为24小时时(其基于医疗的项目,所以24小时是这里的约定)12:00 am-12:59 am格式化为24:00-24:59,而不是00:00 -00:59 ...

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...

这不是一个问题,直到我们查询数据库并按日期排序结果,任何数据之间的12 :00am和12:59 am会显示在列表的结尾,但它应该显示在开头...

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.

干杯

推荐答案

我强烈怀疑你使用的模式错误。我们必须猜测,因为您没有发布任何代码(提示,提示),但我怀疑您使用的是

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

p>

instead of

HH:mm:ss


b $ b

示例代码:

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

此外,正如其他人所指出的, t存储您的值以字符串格式开始...避免字符串转换,无论你可以,因为每个转换都是一个潜在的痛点。

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.

这篇关于Java日期 - 12am存储为24?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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