久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 java 和 .net 之間轉(zhuǎn)換日期 - 休息 2 天

Convert date between java and .net - 2 days off(在 java 和 .net 之間轉(zhuǎn)換日期 - 休息 2 天)
本文介紹了在 java 和 .net 之間轉(zhuǎn)換日期 - 休息 2 天的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我需要將 .NET DateTime 轉(zhuǎn)換為等效的 Java Calendar 表示.

I need to transform a .NET DateTime to an equivalent Java Calendar representation.

.NET DateTime 使用 Jan 1st 0001(.NET 紀(jì)元)以來的 Ticks 作為基礎(chǔ)表示.

The .NET DateTime uses Ticks since Jan 1st 0001 (the .NET epoch) as the underlying representation.

Java GregorianCalendar 使用自 1970 年 1 月 1 日(Java(或 Unix)紀(jì)元)以來的毫秒數(shù).正如預(yù)期的那樣,對于 Java 紀(jì)元之前的日期,該值為負(fù)數(shù).

The Java GregorianCalendar uses milliseconds since Jan 1st 1970 (the Java (or Unix) epoch). The value is negative for dates before the Java epoch, as expected.

Java 紀(jì)元以來,我在這里以毫秒為單位轉(zhuǎn)換 DateTime 表示:

Here I'm transforming the DateTime representation in millis since the Java epoch:

var dt = new DateTime(1,2,3);  //way, way back.
var javaEpoch = new DateTime(1970, 1, 1);

var javaMillis = (dt - javaEpoch).Ticks / TimeSpan.TicksPerMillisecond;

dt.ToString("MM/dd/yyyy").Dump();          // .Dump() is provided by LinqPad. 
javaMillis.Dump();                         // Use Console.WriteLine(...)
                                           // for a regular console app.

這個輸出:

02/03/0001
-62132745600000

02/03/0001
-62132745600000

現(xiàn)在復(fù)制粘貼此 Java 代碼段中的毫秒值:

Now copy-paste the milliseconds value in this Java snippet:

java.util.Calendar cal = new java.util.GregorianCalendar();
cal.setTimeInMillis(-62132745600000L);
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat();
df.applyPattern("MM/dd/yyyy");
System.out.println(df.format(cal.getTime()));

這個輸出:

02/05/0001

我想我的問題是:我應(yīng)該如何從 DateTime 中獲取有效的毫秒值,然后我才能正確地構(gòu)造 Java 日歷?

I guess my question is: How am I supposed to get a valid milliseconds value from a DateTime, from which I can correctly construct a Java Calendar?

...帶有隱含的子問題這里到底發(fā)生了什么?"

...with the implied sub-question "what is really going on in here?"

我在從儒略歷到公歷的缺失日期范圍內(nèi)使用 DateTimeValues(1582 年 10 月 4 日緊隨"1582 年 10 月 15 日).

I played with DateTimeValues around the missing date range from Julian to Gregorian calendar (Oct 4 1582 is "followed" by Oct 15 1582).

對于 1582 年 10 月 15 日之后的日期,轉(zhuǎn)換似乎工作正常.

For dates more recent than Oct 15 1582, the conversion seems to work fine.

...但是在缺少的范圍內(nèi),DateTime 開始(或者更確切地說,開始)表現(xiàn)得滑稽:

...But around the missing range, DateTime starts (or rather, doesn't start) to act funny:

var timespan = new DateTime(1582, 10, 15) - new DateTime(1582, 10, 4);

返回 11 天的 TimeSpan,因此 DateTime 運算符不考慮漏洞.是什么賦予了?我認(rèn)為底層實現(xiàn)是基于System.Globalization.GregorianCalendar.

returns a TimeSpan of 11 days, so the hole is not taken into consideration by the DateTime operators. What gives? I thought the underlying implementation is based on System.Globalization.GregorianCalendar.

推薦答案

回答為什么":

來自(反編譯 - 感謝 dotPeek!).NET 4 源代碼(評論是我的):

From the (decompiled - thanks dotPeek!) .NET 4 source code (comments are mine):

public static DateTime operator -(DateTime d, TimeSpan t)
{
  //range checks
  long internalTicks = d.InternalTicks;
  long num = t._ticks;
  if (internalTicks < num || internalTicks - 3155378975999999999L > num)
    throw new ArgumentOutOfRangeException("t", 
            Environment.GetResourceString("ArgumentOutOfRange_DateArithmetic"));

    else

    //plain arithmetic using the Ticks property of the two dates.
    return new DateTime((ulong) (internalTicks - num) | d.InternalKind);
}

是的,對于 DateTime 運算符,絕對沒有特殊的公歷"處理.

So yeah, absolutely no special 'gregorian' treatment for DateTime operators.

關(guān)于如何修復(fù)":

我最終使用了一些類似的東西:(偽 Java)

I ended up using something along these lines: (pseudo-Java)

Calendar cal = new GregorianCalendar();
cal.set(dt.Year, dt.Month - 1 /*it's 0-based*/, dt.Day, dt.Hour, dt.Minute, dt.Second);
cal.set(Calendar.MILLISECOND, dt.Millisecond);

這篇關(guān)于在 java 和 .net 之間轉(zhuǎn)換日期 - 休息 2 天的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當(dāng)前星期幾的值)
主站蜘蛛池模板: 超碰国产在线 | 中文字幕在线观看 | 中文亚洲视频 | 日韩国产一区二区三区 | 中文字幕亚洲一区 | 中文字幕日韩欧美一区二区三区 | 蜜臀久久99精品久久久久久宅男 | 黄色一级网| 欧美精品一区二区三区在线播放 | 黄色精品视频网站 | 日韩在线播放网址 | 国产精品亚洲第一 | 91免费在线看 | 二区三区视频 | 国产日产精品一区二区三区四区 | 精品网站999| 久久久精 | 久久伊人一区二区 | 99久久精品免费看国产四区 | 久久精品国产99国产 | 91精品国产91久久综合桃花 | 亚洲欧美中文日韩在线v日本 | 国产视频福利在线观看 | 久久综合一区 | 日日干日日操 | 国产精品久久在线 | 99亚洲精品 | 日韩欧美在线视频播放 | 成人自拍视频 | 日韩欧美网 | 天天天操 | 久久久久久久久久久久亚洲 | 久久网一区二区三区 | 天天拍天天插 | 国产欧美日韩久久久 | 国产精品久久久久久久久久 | 久久综合久色欧美综合狠狠 | 污视频免费在线观看 | 亚洲免费在线 | 色婷婷久久久久swag精品 | 亚洲女人天堂网 |