Java 8时间-等效于.NET DateTime.MaxValue.Ticks [英] Java 8 Time - Equivalent of .NET DateTime.MaxValue.Ticks

查看:20
本文介绍了Java 8时间-等效于.NET DateTime.MaxValue.Ticks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在下面包括了整个方法,但实际上挑战在于在Java 8中模拟DateTime.MaxValue.Ticks.我也不知道Java中的等效于".ToString(" D19)".

I've included the whole method below, but really the challenge is simulating DateTime.MaxValue.Ticks in Java 8. I also don't know the equivalent of ".ToString("D19") in Java.

我认为我已经找到了开始的方法,这是通过使用Instant.MAX.toEpochMilli()进行的,然后我可以将其乘以10000得到Ticks.可悲的是,这个简单的语句引发了一个异常,所以它不是入门级的

I thought I had figured out how to begin, which was by using Instant.MAX.toEpochMilli(), which I could then multiply by 10000 to get Ticks. Sadly, this simple statement throws an exception, so it's a non-starter:

捕获:java.lang.ArithmeticException:长时间溢出

Caught: java.lang.ArithmeticException: long overflow

这是原始方法.它用于查询Azure存储表的历史指标.

Here is the original method. It's used to query Azure Storage Tables for historical metrics.

// Creates a TableQuery for getting metrics by timestamp
private static TableQuery GenerateMetricTimestampQuery(string partitionKey, DateTime startTime, DateTime endTime)
{
    return GenerateMetricQuery(
        partitionKey,
        (DateTime.MaxValue.Ticks - endTime.Ticks + 1).ToString("D19") + "__",
        (DateTime.MaxValue.Ticks - startTime.Ticks).ToString("D19") + "__");
}

以下是RowKey字段值的示例:

Here is an example of a RowKey field value:

2519303419199999999 __

2519303419199999999__

我已经花了一天的时间,我很沮丧.任何帮助将不胜感激.

I've spent a day on this and I'm pretty stumped. Any help would be greatly appreciated.

如果可能的话,我希望在没有JodaTime的情况下执行此操作.

If possible, I would prefer to do this without JodaTime.

UPDATE1 ***基于注释,这是Java中异常的示例.

UPDATE1*** Based on a comment, here is an example of the exception in Java.

import java.time.Instant;
public class Tester {
    public static void main(String[] args){
        System.out.println(Instant.MAX.toEpochMilli());
    }
}

推荐答案

UPDATE 原始答案未解决Java纪元(1970)与.NET刻度(0001)之间的偏移量差异的问题..更正了!

UPDATE Original answer didn't account for offset difference between Java epoch (1970) and .NET ticks (0001). Corrected!

作为参考, Long.MAX_VALUE (Java)为:
9,223,372,036,854,775,807

For reference, Long.MAX_VALUE (Java) is:
9,223,372,036,854,775,807

在.NET中, DateTime.MaxValue 是:
9999-12-31 23:59:59.9999999
3,155,378,975,999,999,999 滴答声 1 (〜1/3长)

In .NET, DateTime.MaxValue is:
9999-12-31 23:59:59.9999999
3,155,378,975,999,999,999 ticks1 (~ 1/3 of long)

在Java 8中, Instant.MAX 是:
+ 1000000000-12-31 23:59:59.999999999
31,556,889,864,403,199,999,999,999 纳米(长时间溢出)
315,568,898,644,031,999,999,999 刻度 2 (长时间溢出)
31,556,889,864,403,199,999 米利斯(长时间溢出)
31,556,889,864,403,199 (〜1/292,长)

In Java 8, Instant.MAX is:
+1000000000-12-31 23:59:59.999999999
31,556,889,864,403,199,999,999,999 nanos (overflows long)
315,568,898,644,031,999,999,999 ticks2 (overflows long)
31,556,889,864,403,199,999 millis (overflows long)
31,556,889,864,403,199 seconds (~ 1/292 of long)

作为参考,您的 2519303419199999999 的值为:
2016-08-23 13:28:00
636,075,556,800,000,000 滴答声 1 (〜长的1/14)
14,719,588,800,000,000 勾号 2 (〜1/626的长号)
1)从0001-01-01(.NET)开始 2)自1970年1月1日(Java)

For reference, your value of 2519303419199999999 is:
2016-08-23 13:28:00
636,075,556,800,000,000 ticks1 (~ 1/14 of long)
14,719,588,800,000,000 ticks2 (~ 1/626 of long)
1) Since 0001-01-01 (.NET)     2) Since 1970-01-01 (Java)

如您所见,滴答"中的 Instant.MAX 放入 long 中.甚至毫秒都不合适.

As you can see, Instant.MAX in "ticks" will not fit in a long. Not even milliseconds will fit.

更重要的是, Instant.MAX DateTime.MaxValue 的值不同.

我建议您为该值创建一个常量,例如

I would suggest you just create a constant for the value, e.g.

public static final long DATETIME_MAXVALUE_TICKS = 3155378975999999999L; // .NET: DateTime.MaxValue.Ticks

这样,您将获得与.NET代码相同的字符串值:

That way you'll get same string values as you .NET code:

public static final long EPOCH_OFFSET = 62135596800L; // -Instant.parse("0001-01-01T00:00:00Z").getEpochSecond()

private static long getTicks(Instant instant) {
    long seconds = Math.addExact(instant.getEpochSecond(), EPOCH_OFFSET);
    long ticks = Math.multiplyExact(seconds, 10_000_000L);
    return Math.addExact(ticks, instant.getNano() / 100);
}

public static void main(String[] args) {
    Instant startTime = Instant.parse("2016-08-23T13:28:00Z");
    String s = String.format("%19d", DATETIME_MAXVALUE_TICKS - getTicks(startTime));
    System.out.println(s);
}

输出:
2519303419199999999

这篇关于Java 8时间-等效于.NET DateTime.MaxValue.Ticks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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