相关文章推荐
朝气蓬勃的柿子  ·  js解析xml字符串 - ...·  1 年前    · 
public static String Local2UTC(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("gmt")); String gmtTime = sdf.format(new Date()); return gmtTime; * UTC时间 ---> 当地时间 * @param utcTime UTC时间 * @return public static String utc2Local(String utcTime) { SimpleDateFormat utcFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//UTC时间格式 utcFormater.setTimeZone(TimeZone.getTimeZone("UTC")); Date gpsUTCDate = null; try { gpsUTCDate = utcFormater.parse(utcTime); } catch (ParseException e) { e.printStackTrace(); SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//当地时间格式 localFormater.setTimeZone(TimeZone.getDefault()); String localTime = localFormater.format(gpsUTCDate.getTime()); return localTime; /** * 当地时间 ---> UTC时间 * @return */ public static String Local2UTC(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setTimeZone(TimeZone.get 1 UTC (世界标准 时间 ) 协调世界时,又称世界标准 时间 或世界协调 时间 ,简称 UTC (从英文“Coordinated Universal Time”/法文“TempsUniversel Coordonné”而来),是最主要的世界 时间 标准,其以原子时秒长为基础,在时刻上尽量接近于格 林尼治标准 时间 。 2 GMT(格林尼治平时) 格林尼治平时(... //是Time zone information的缩写,描述时区的一些重要信息 FTZI: PRegTZIInfo; function GetSelfTimeZoneInformation: TTimeZoneInformation; public constructor Create; destructor Destroy; override; function UTC ToLocalDateTime(const A UTC : TDateTime; var ALocalDateTime: TDateTime): Boolean; function LocalDateTimeTo UTC (const ALocalDateTime: TDateTime; var A UTC : TDateTime): Boolean; public property TimeZoneName: string read FTimeZoneName write FTimeZoneName; property Display: string read FDisplay write FDisplay; property Dlt: string read FDlt write FDlt; property Std: string read FStd write FStd; property TZI: PRegTZIInfo read FTZI write FTZI; {* 所有时区管理对象 *} TTimeZones = class private FTimeZoneKeyPath: string; FTimeZoneList: TStringList; FDefaultTimeZone: TTimeZone; procedure CollectTimeZone; procedure DestoryTimeZones; procedure CheckISDefaultTimeZone(ATimeZone: TTimeZone); public constructor Create; destructor Destroy; override; function FindTimeZone(const ADisplay: string): TTimeZone; public property TimeZoneList: TStringList read FTimeZoneList; property DefaultTimeZone: TTimeZone read FDefaultTimeZone; implementation { TTimeZones } procedure TTimeZones.CheckISDefaultTimeZone(ATimeZone: TTimeZone); DefaultTimeZone: TTimeZoneInformation; begin GetTimeZoneInformation(DefaultTimeZone); if (ATimeZone.TZI.Bias = DefaultTimeZone.Bias) and (ATimeZone.Std = DefaultTimeZone.StandardName) then FDefaultTimeZone := ATimeZone; procedure TTimeZones.CollectTimeZone; reg, tempReg: TRegistry; tempKeyPath: string; tempTimeZoneStrings: TStrings; iCir: Integer; tempTimeZone: TTimeZone; begin reg := TRegistry.Create; reg.RootKey := HKEY_LOCAL_MACHINE; reg.OpenKey(FTimeZoneKeyPath, False); tempTimeZoneStrings := TStringList.Create; reg.GetKeyNames(tempTimeZoneStrings); for iCir := 0 to tempTimeZoneStrings.Count - 1 do begin tempKeyPath := FTimeZoneKeyPath + '\' + tempTimeZoneStrings.Strings[iCir]; tempReg := TRegistry.Create; tempReg.RootKey := HKEY_LOCAL_MACHINE; tempReg.OpenKey(tempKeyPath, False); tempTimeZone := TTimeZone.Create; tempTimeZone.TimeZoneName := tempTimeZoneStrings.Strings[iCir]; tempTimeZone.Display := tempReg.ReadString('Display'); tempTimeZone.Std := tempReg.ReadString('Std'); tempTimeZone.Dlt := tempReg.ReadString('Dlt'); tempReg.ReadBinaryData('TZI', tempTimeZone.TZI^, SizeOf(TRegTZIInfo)); FTimeZoneList.AddObject(tempTimeZone.Display, tempTimeZone); if FDefaultTimeZone = nil then CheckISDefaultTimeZone(tempTimeZone); finally tempReg.CloseKey; tempReg.Free; finally tempTimeZoneStrings.Free; finally reg.CloseKey; reg.Free; constructor TTimeZones.Create; begin FTimeZoneKeyPath := '\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones'; FTimeZoneList := TStringList.Create; FTimeZoneList.Sorted := True; FDefaultTimeZone := nil; CollectTimeZone; procedure TTimeZones.DestoryTimeZones; iCir: Integer; tempTimeZone: TTimeZone; begin for iCir := 0 to FTimeZoneList.Count - 1 do begin tempTimeZone := TTimeZone(FTimeZoneList.Objects[iCir]); tempTimeZone.Free; FTimeZoneList.Clear; FTimeZoneList.Free; destructor TTimeZones.Destroy; begin DestoryTimeZones(); inherited; function TTimeZones.FindTimeZone(const ADisplay: string): TTimeZone; iIndex: Integer; begin Result := nil; FTimeZoneList.Sort; if FTimeZoneList.Find(ADisplay, iIndex) then begin Result := TTimeZone(FTimeZoneList.Objects[iIndex]); { TTimeZone } constructor TTimeZone.Create; begin FTZI := GetMemory(SizeOf(TRegTZIInfo)); FillMemory(FTZI, SizeOf(TRegTZIInfo), 0); destructor TTimeZone.Destroy; begin FreeMemory(FTZI); inherited; function TTimeZone.GetSelfTimeZoneInformation: TTimeZoneInformation; begin GetTimeZoneInformation(Result); Result.Bias := TZI^.Bias; Result.StandardBias := TZI^.StandardBias; Result.StandardDate := TZI^.StandardDate; Result.DaylightBias := TZI^.DaylightBias; Result.DaylightDate := TZI^.DaylightDate; function TTimeZone.LocalDateTimeTo UTC (const ALocalDateTime: TDateTime; var A UTC : TDateTime): Boolean; tempLocalToLocal: TDateTime; iMilliSecond: Int64; begin Result := UTC ToLocalDateTime(ALocalDateTime, tempLocalToLocal); if Result then begin if tempLocalToLocal > ALocalDateTime then begin iMilliSecond := MilliSecondsBetween(tempLocalToLocal, ALocalDateTime); A UTC := IncMilliSecond(ALocalDateTime, iMilliSecond * -1); end else begin iMilliSecond := MilliSecondsBetween(ALocalDateTime, tempLocalToLocal); A UTC := IncMilliSecond(ALocalDateTime, iMilliSecond); Result := True; function TTimeZone. UTC ToLocalDateTime(const A UTC : TDateTime; var ALocalDateTime: TDateTime): Boolean; TimeZone: TTimeZoneInformation; st UTC , stLC: SYSTEMTIME; begin Result := False; TimeZone := GetSelfTimeZoneInformation; DateTimeToSystemTime(A UTC , st UTC ); if SystemTimeToTzSpecificLocalTime(@TimeZone, st UTC , stLC) then begin ALocalDateTime := SystemTimeToDateTime(stLC); Result := True; import android .content.Context; import android .text.TextUtils; import android .text.format.DateFormat; import com.peopleapp.en.R; import java.text.ParseException; im...
public static String get UTC FromLocalTime_(int hour, int minute, int second) { StringBuffer UTC TimeBuffer = new StringBuffer(); DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:...
在有些软件 ,可能需要展示一些 时间 信息,而这些信息可能是Server以 UTC 格式或Unix timestamp 格式推送过来的,终端可能需要将这些 转换 为本地 时间 展示。 终端的制式可能是12小时制、也可能是24小时制的 今天就遇到将 utc 时间 格式 转换 为GMT  HH:返回的是24小时制的 时间 hh:返回的是12小时制的 时间 bjt_time = utc _time.astimezone(pytz.timezone('Asia/Shanghai')) print(" UTC 时间 :", utc _time) print("北京 时间 :", bjt_time) 注意:需要先安装pytz库,可以使用pip进行安装。