Java中Calendar实例的时间部分重置

发布于 2024-10-06 09:28:00 字数 549 浏览 8 评论 0原文

有Java代码,它在2个不同的环境中运行,并使用JdbcTemplate将记录插入到DB

两个环境的运行结果都不同。特别是对于 Date 字段。

在第一个环境(Oracle XE)上,它会生成记录:

"12/03/2010";191094;"71697211000";3229;880323202;NULL;0;1;0;NULL;0;NULL

第二个环境(Oracle 非 XE):

"12/03/2010 12:00:00";191094;"71697211000";3229;880323202;NULL;0;1;0;NULL;0;NULL

NLS_DATE_FORMAT(如果很重要)第一个环境是 DD-MON- RR, 第二个环境是 DD-MON-RRRR

问题是,Oracle 设置可能会影响第二个环境日期格式是另一种吗?

There is Java code,which is ran on 2 different environments and inserts records with JdbcTemplate to DB.

Results of its running are different for both envs. Particularly,for Date fields.

On first environment(Oracle XE) it produces record:

"12/03/2010";191094;"71697211000";3229;880323202;NULL;0;1;0;NULL;0;NULL

Second environment(Oracle non XE):

"12/03/2010 12:00:00";191094;"71697211000";3229;880323202;NULL;0;1;0;NULL;0;NULL

NLS_DATE_FORMAT(if it's crucial) for first env is DD-MON-RR,
for second env is DD-MON-RRRR

The question is,what Oracle settings may affect,that second env Date format is another?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你的笑 2024-10-13 09:28:00

应在 Java 代码中设置以下日历属性:

  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
  cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
  cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
  cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));

而不是:

  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.HOUR_OF_DAY, 0);
  cal.set(Calendar.MINUTE, 0);
  cal.set(Calendar.SECOND, 0);
  cal.set(Calendar.MILLISECOND, 0);

should set following Calendar properties in Java code:

  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
  cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
  cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
  cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));

instead of:

  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.HOUR_OF_DAY, 0);
  cal.set(Calendar.MINUTE, 0);
  cal.set(Calendar.SECOND, 0);
  cal.set(Calendar.MILLISECOND, 0);
深居我梦 2024-10-13 09:28:00

根据文档HH指的是12小时时间。您检索的时间中的 12 点是午夜 12 点。您想要的是 HH24,它为您提供 24 小时时间,从午夜 00 点开始。

According to the documentation, HH refers to a 12-hour time. The 12 in the time you're retrieving is 12 midnight. What you want is HH24, which ges you a 24-hour time, starting at 00 for midnight.

烟雨凡馨 2024-10-13 09:28:00

如果您不想显示时间部分,请不要包含包含时间部分的格式字符串(“HH:MI:SS”)。

您已将时间部分重置为午夜,基本上...无法区分设置为午夜的日历日期日历 code> 或 Date “没有”时间部分 - 因为不存在仅包含日期部分的 Calendar/Date 这样的概念。

现在,您可能可以将其存储在数据库中,具体取决于您可以使用的类型 - 但 java.util.Date 和 java.util.Calendar 始终表示点及时,而不仅仅是日期。

它显示 12 而不是 00 的原因是因为您使用的是“HH”而不是“HH24”,根据 lacqui 的回答。我假设您根本不想看到时间,因为您已将其重置为午夜......

If you don't want to show the time part, don't include a format string which includes the time part ("HH:MI:SS").

You've reset the time part to midnight, basically... there's no way of differentiating between a Calendar or Date set to exactly midnight and a Calendar or Date "without" a time part - because there's no such concept as a Calendar/Date with only a date part.

Now you may be able to have that in the database, depending on what types are available to you - but java.util.Date and java.util.Calendar always represent points in time, not just dates.

The reason it's showing 12 instead of 00 is because you're using "HH" instead of "HH24", as per lacqui's answer. I assume you don't really want to see the time at all though, given that you'll have reset it to midnight...

信仰 2024-10-13 09:28:00

我建议您扩展日历类之一,如下所示:

public class CalendarDateOnly extends GregorianCalendar {

    public static Calendar getInstance() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE,      cal.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND,      cal.getActualMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));
        return cal;
    }
}

以这种方式实例化:

Calendar june30 = CalendarDateOnly.getInstance();
june30.set(2000, Calendar.JUNE, 30);

I'd recommend you extend one of the Calendar classes like this:

public class CalendarDateOnly extends GregorianCalendar {

    public static Calendar getInstance() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE,      cal.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND,      cal.getActualMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));
        return cal;
    }
}

Instantiate in this way:

Calendar june30 = CalendarDateOnly.getInstance();
june30.set(2000, Calendar.JUNE, 30);
故事与诗 2024-10-13 09:28:00
/*
 * Date : 2015-07-09
 * Author : Bhuwan Prasad Upadhyay
 */
package com.developerbhuwan.date.utils;

import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author developerbhuwan
 */
public class CalenderUtils {

    public static Calendar getNewCalendarInstance() {
        Calendar calendar = Calendar.getInstance();
        return resetCalender(calendar);
    }

    public static Date resetDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return resetCalender(calendar).getTime();
    }

    public static Calendar resetCalender(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar;
    }

    public static void setTimeInCalendar(Calendar calendar, Date date) {
        calendar.setTime(resetDate(date));
    }

}
/*
 * Date : 2015-07-09
 * Author : Bhuwan Prasad Upadhyay
 */
package com.developerbhuwan.date.utils;

import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author developerbhuwan
 */
public class CalenderUtils {

    public static Calendar getNewCalendarInstance() {
        Calendar calendar = Calendar.getInstance();
        return resetCalender(calendar);
    }

    public static Date resetDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return resetCalender(calendar).getTime();
    }

    public static Calendar resetCalender(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar;
    }

    public static void setTimeInCalendar(Calendar calendar, Date date) {
        calendar.setTime(resetDate(date));
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文