Int96Value to Date字符串 [英] Int96Value to Date string

查看:60
本文介绍了Int96Value to Date字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在读取实木复合地板文件(使用Scala)时,我将时间戳字段读回为:

When reading a parquet file (using Scala) I read the timestamp field back as:

Int96Value{Binary{12 constant bytes, [0, 44, 84, 119, 54, 49, 0, 0, -62, -127, 37, 0]}}

如何将其转换为日期字符串?

How can I convert it to a date string?

推荐答案

java.time支持儒略日.

java.time supports Julian days.

感谢ygor进行研究并找出如何解释数组的12个字节.

Credits to ygor for doing the research and finding out how to interpret the 12 bytes of your array.

    byte[] int96Bytes = { 0, 44, 84, 119, 54, 49, 0, 0, -62, -127, 37, 0 };

    // Find Julian day
    int julianDay = 0;
    int index = int96Bytes.length;
    while (index > 8) {
        index--;
        julianDay <<= 8;
        julianDay += int96Bytes[index] & 0xFF;
    }

    // Find nanos since midday (since Julian days start at midday)
    long nanos = 0;
    // Continue from the index we got to
    while (index > 0) {
        index--;
        nanos <<= 8;
        nanos += int96Bytes[index] & 0xFF;
    }

    LocalDateTime timestamp = LocalDate.MIN
            .with(JulianFields.JULIAN_DAY, julianDay)
            .atTime(LocalTime.NOON)
            .plusNanos(nanos);
    System.out.println("Timestamp: " + timestamp);

此打印:

时间戳:2017-10-24T03:01:50

Timestamp: 2017-10-24T03:01:50

我不愿意手动将字节数组转换为 int long ,但我不知道Parquet是否足以使用转换可能在那里可用.如果可以的话,请使用它们.

I’m not happy about converting your byte array to an int and a long by hand, but I don’t know Parquet will enough to use the conversions that are probably available there. Use them if you can.

使用哪个 LocalDate 作为起点并不重要,因为无论如何我们都将其更改为正确的儒略日,所以我选择了 LocalDate.MIN 只是为了选择一个.

It doesn’t matter which LocalDate we use as starting point since we are changing it to the right Julian day anyway, so I picked LocalDate.MIN just to pick one.

在我阅读文档的方式中,儒略日总是在当地时区,也就是说,没有时区被理解,并且它们总是从中午开始(而不是午夜).

The way I read the documentation, Julian days are always in the local time zone, that is, no time zone is understood, and they always start at midday (not midnight).

链接:: 查看全文

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