数据库DateTime毫秒如果在Java-11中使用ZonedDateTime使用纳秒,则默认将其截断为0毫微秒 [英] Databse DateTime milli & nano seconds are truncated by default if its 0s while using it in java-11 using ZonedDateTime

查看:337
本文介绍了数据库DateTime毫秒如果在Java-11中使用ZonedDateTime使用纳秒,则默认将其截断为0毫微秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Oracle数据库中获取日期时间,并使用ZonedDateTime在java-11中进行解析,如下所示:

I am fetching datetime from oracle db and parsing in java-11 using ZonedDateTime as below:

  1. oracle-> 1/19/2020 06:09:46.038631 PM

java ZonedDateTime 0/p-> 2020-01-19T18:09:46.038631Z[UTC]

java ZonedDateTime 0/p --> 2020-01-19T18:09:46.038631Z[UTC]

oracle-> 1/19/2011 4:00:00.000000 AM

oracle --> 1/19/2011 4:00:00.000000 AM

java ZonedDateTime o/p-> 2011-01-19T04:00Z[UTC](因此,此处默认情况下将0截断. 但是,我的要求是像#1一样具有一致的固定长度o/p.)

java ZonedDateTime o/p --> 2011-01-19T04:00Z[UTC] (So, here the 0s are truncated by default. However, my requirement is to have consistent fixed length o/p like #1.)

期望的Java ZonedDateTime o/p-> 2011-01-19T04:00:00.000000Z[UTC]

expected java ZonedDateTime o/p --> 2011-01-19T04:00:00.000000Z[UTC]

但是,我没有找到任何日期api方法来实现高于预期的o/p.除了操纵字符串,还有其他方法可以将尾随的0保留为固定长度. 我们在应用程序中具有一致的ZonedDateTime类型,因此我们不希望更改该类型.

However, I didnt find any date api methods to achieve above expected o/p. Instead of manipulating string, is there any way to preserve the trailing 0s with fixed length. We have consistent ZonedDateTime type in application, so we do not prefer to change that.

推荐答案

我们在应用程序中具有一致的ZonedDateTime类型,因此我们没有 宁愿更改它.

We have consistent ZonedDateTime type in application, so we do not prefer to change that.

为什么您认为2011-01-19T04:00Z[UTC]不一致?日期时间对象应该仅保留日期,时间和时区信息(并提供用于操作的方法/功能).它不应该存储任何格式信息;否则,它将违反单一责任原则.格式化应由格式化类处理,例如DateTimeFormatter(对于现代的日期时间API, ),DateFormat(适用于旧版java.util日期时间API)等

Why do you think 2011-01-19T04:00Z[UTC] is inconsistent? A date-time object is supposed to hold (and provide methods/functions to operate with) only the date, time, and time-zone information. It is not supposed to store any formatting information; otherwise, it will violate the Single-responsibility principle. The formatting should be handled by a formating class e.g. DateTimeFormatter (for modern date-time API), DateFormat (for legacy java.util date-time API) etc.

每个类都应该覆盖toString()函数;否则,Object#toString将在其对象被打印时返回. ZonedDateTime具有日期,时间和时区信息.下面给出了其时间部分toString()的实现方式:

Every class is supposed to override the toString() function; otherwise, Object#toString will be returned when its object will be printed. A ZonedDateTime has date, time and time-zone information. Given below is how its toString() for time-part has been implemented:

@Override
public String toString() {
    StringBuilder buf = new StringBuilder(18);
    int hourValue = hour;
    int minuteValue = minute;
    int secondValue = second;
    int nanoValue = nano;
    buf.append(hourValue < 10 ? "0" : "").append(hourValue)
        .append(minuteValue < 10 ? ":0" : ":").append(minuteValue);
    if (secondValue > 0 || nanoValue > 0) {
        buf.append(secondValue < 10 ? ":0" : ":").append(secondValue);
        if (nanoValue > 0) {
            buf.append('.');
            if (nanoValue % 1000_000 == 0) {
                buf.append(Integer.toString((nanoValue / 1000_000) + 1000).substring(1));
            } else if (nanoValue % 1000 == 0) {
                buf.append(Integer.toString((nanoValue / 1000) + 1000_000).substring(1));
            } else {
                buf.append(Integer.toString((nanoValue) + 1000_000_000).substring(1));
            }
        }
    }
    return buf.toString();
}

如您所见,仅当第二部分和nano部分大于0时,它们才包含在返回的字符串中.这意味着如果要在输出字符串中使用这些零(第二个和第二个),则需要使用格式化类.下面是一个示例:

As you can see, the second and nano parts are included in the returned string only when they are greater than 0. It means that you need to use a formatting class if you want these (second and nano) zeros in the output string. Given below is an example:

    import java.time.LocalDateTime;
    import java.time.ZoneOffset;
    import java.time.ZonedDateTime;
    import java.time.format.DateTimeFormatter;
    import java.time.format.DateTimeFormatterBuilder;
    import java.util.Locale;

    public class Main {
        public static void main(String[] args) {
            String input = "1/19/2011 4:00:00.000000 AM";

            // Formatter for input string
            DateTimeFormatter inputFormatter = new DateTimeFormatterBuilder()
                                                .parseCaseInsensitive()
                                                .appendPattern("M/d/u H:m:s.n a")
                                                .toFormatter(Locale.ENGLISH);

            ZonedDateTime zdt = LocalDateTime.parse(input, inputFormatter).atZone(ZoneOffset.UTC);

            // Print `zdt` in default format i.e. the string returned by `zdt.toString()`
            System.out.println(zdt);

            // Formatter for input string
            DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.nnnnnnz");
            String output = zdt.format(outputFormatter);
            System.out.println(output);
        }
    }

输出:

2011-01-19T04:00Z
2011-01-19T04:00:00.000000Z

令人回味的食物:

public class Main {
    public static void main(String[] args) {
        double d = 5.0000;
        System.out.println(d);
    }
}

您期望从上面给出的代码中得到什么输出? 5.0表示与5.0000不同的值吗?您将如何打印5.0000? [提示:检查String#formatNumberFormatBigDecimal等.

What output do you expect from the code given above? Does 5.0 represent a value different from 5.0000? How will you print 5.0000? [Hint: Check String#format, NumberFormat, BigDecimal etc.]

这篇关于数据库DateTime毫秒如果在Java-11中使用ZonedDateTime使用纳秒,则默认将其截断为0毫微秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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