
java時區轉換夏令時_實現時區的轉換--涉及到冬令時和夏令時
的時候
#時區轉換
主要是?來轉換時區?的,特別涉及到冬令時和夏令時的區域,這樣會很煩,所以需要?個?法來轉換
這個主要是創建?些模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tools.TimeZone
{
///
/// 國家時區類型
///
public enum ZoneType
{
Same = 0,//普通的時區轉換
Summer = 1,//啟?夏時令 在夏令時的時候?冬令時早1個?時
}
///
/// 時區
/
//
public class TimeZoneModel
{
///
/// 冬令時GMT
///
public int GMT { get; t; }
///
/// 這個國家應?的時區是什么
///
public ZoneType ZoneType { get; t; }
/
// 時區名稱
///
public string ZoneName { get; t; }
///
/// 時區備注
///
public string ZoneRemark { get; t; } ///
/// 夏時令開始時間
///
public TimeNode BeginDate { get; t; } ///
/// 夏時令結束時間
/
//
public TimeNode EndDate { get; t; } }
///
/// 時間節點
///
public class TimeNode
{
///
/// ?份
///
public int Month { get; t; }
/
//
/// 排序?式
///
public Sort Sort { get; t; }
///
/// 第?個
///
public int Num { get; t; }
/// 周?
///
public DayOfWeek DayOfWeek { get; t; }
/
//
/// ?點開始
///
public int Hours { get; t; } = 0;//?般是0點開始}
///
/// 查找?式
///
public enum Sort
{
///
/// 倒數第?個
/
//
desc = 0,
///
/// 正數第?個
///
asc=1,
}
}
下?的是具體的?法
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tools.TimeZone
public static class DateZoneHelper
{
private static List _zoneList;
private static readonly string PATH = "TimeZone.json";
public static List ZoneList
{
get
{
if (_zoneList == null)
{
//從數據源中獲取數據
//string Path = "TimeZone.json";
if (!File.Exists(PATH))
{
_zoneList = new List();
}
el
{
string str = File.ReadAllText(PATH);
//JArray array = JArray.Par(str);
_zoneList=JsonConvert.DerializeObject>(str);
}
}
return _zoneList;
}
}
public static TimeZoneModel GetZone(string ZoneName)
{
return ZoneList.SingleOrDefault(a => a.ZoneName == ZoneName);
}
public static DateTimeOfft ConvertDate(this DateTimeOfft dateTime, string ZoneName) {
TimeZoneModel zone = GetZone(ZoneName);//獲取當前時區的參數
return dateTime.ConvertDate(zone);
}
public static DateTimeOfft ConvertDate(this DateTimeOfft dateTime, TimeZoneModel zone)
{
if (zone==null)
{
return DateTimeOfft.MinValue;
}
DateTimeOfft date = DateTimeOfft.MinValue;
DateTime dtUTC = dateTime.UtcDateTime;
switch (zone.ZoneType)
{
ca ZoneType.Summer:
DateTime dtToDate = dtUTC.AddHours(zone.GMT+1);//假設當前時間是夏令時
DateTime BeginDate = GetDateByNodeTime(dtToDate.Year,zone.BeginDate).AddHours(1);//夏令時開始時間DateTime EndDate = GetDateByNodeTime(dtToDate.Year, zone.EndDate);//夏令時結束時間
if (BeginDate<= dtToDate&& dtToDate< EndDate)
{
date= dateTime.ToOfft(new TimeSpan(TimeSpan.TicksPerHour * (zone.GMT+1)));
}
el
{
date = dateTime.ToOfft(new TimeSpan(TimeSpan.TicksPerHour * (zone.GMT)));
//date = new DateTimeOfft(dtUTC.AddHours(zone.GMT), new TimeSpan(TimeSpan.TicksPerHour * zone.GMT)); }
break;
ca ZoneType.Same:
default:
DateTime dtTemp = dtUTC.AddHours(zone.GMT);
date = dateTime.ToOfft(new TimeSpan(TimeSpan.TicksPerHour * (zone.GMT)));
//date = new DateTimeOfft(dtTemp, new TimeSpan(TimeSpan.TicksPerHour * zone.GMT));
break;
}
return date;