第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將字符串 (HH:mm) 設(shè)置為當前日期的 UTC 時間并將其轉(zhuǎn)換為本地時間

如何將字符串 (HH:mm) 設(shè)置為當前日期的 UTC 時間并將其轉(zhuǎn)換為本地時間

一只甜甜圈 2024-01-28 17:09:36
我需要將 (HH:mm) 格式的字符串轉(zhuǎn)換為本地時區(qū),該字符串應(yīng)該是 UTC 時間。如何將當前日期添加到字符串并將其轉(zhuǎn)換為當?shù)貢r間。我嘗試過使用日歷String utcTimeString = "06:00";SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());sdf.setTimeZone(TimeZone.getTimeZone("UTC"));Calendar now = Calendar.getInstance(Locale.getDefault());now.setTime(sdf.parse(utcTimeString));
查看完整描述

3 回答

?
慕慕森

TA貢獻1856條經(jīng)驗 獲得超17個贊

強烈建議您使用現(xiàn)代 API 來獲取日期、時間、時區(qū)、偏移量、日歷等:

java.time

這樣做,非常容易

  1. 解析你收到的時間

  2. 獲取當前日期和

  3. 將它們組合成具有特定時區(qū)的日期時間表示

看這個小例子:

public static void main(String[] args) {

    // create a time object from the String

    LocalTime localTime = LocalTime.parse("06:00", DateTimeFormatter.ofPattern("HH:mm"));

    // print it once in an ISO format

    System.out.println(localTime.format(DateTimeFormatter.ISO_TIME));

    // receive the date of today

    LocalDate today = LocalDate.now();

    // then use the date and the time object to create a zone-aware datetime object

    ZonedDateTime zdt = LocalDateTime.of(today, localTime).atZone(ZoneId.of("UTC"));

    // print it

    System.out.println(zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));

}

輸出是


06:00:00

2019-11-04T06:00:00Z[UTC]

您可以根據(jù)需要使用不同的DateTimeFormatters 進行格式化。


查看完整回答
反對 回復(fù) 2024-01-28
?
肥皂起泡泡

TA貢獻1829條經(jīng)驗 獲得超6個贊

嘗試如下所示。


public String getDateTimeInUTC(String yourTime){ 

    Calendar cal = Calendar.getInstance();

    SimpleDateFormat currentDate= new SimpleDateFormat("MMM dd, yyyy ");


    String currentDateTime = currentDate.format(cal.getTime())+yourTime; // here concate your time with current date.

    System.out.println("Current date with given time: "+currentDateTime);


    SimpleDateFormat df = new SimpleDateFormat("MMM dd, yyyy HH:mm", Locale.ENGLISH);

    df.setTimeZone(TimeZone.getTimeZone("UTC"));


    Date date = null;

    try {

       date = df.parse(currentDateTime);

    } catch (ParseException e) {

       e.printStackTrace();

    }

    df.setTimeZone(TimeZone.getDefault());

    String formattedDate = df.format(date);


    return formattedDate;

}

getDateTimeInUTC像下面這樣調(diào)用


String strTime = "12:10"; // your string time in HH:mm format

String finalDateTime = getDateTimeInUTC(strTime);

System.out.println("Final date-time in UTC: "+finalDateTime);

輸出:


Current date with  given time: Nov 04, 2019 12:10

Final date-time in UTC: Nov 04, 2019 18:10


查看完整回答
反對 回復(fù) 2024-01-28
?
素胚勾勒不出你

TA貢獻1827條經(jīng)驗 獲得超9個贊

您可以查看一下:


Calendar calendar = Calendar.getInstance();

calendar.setTime(new Date());


//change the format according to your need

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");


//Here you say to java the initial timezone. This is the secret

sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

//Will print in UTC

System.out.println(sdf.format(calendar.getTime()));    


//Here you set to your timezone

sdf.setTimeZone(TimeZone.getDefault());

//Will print on your default Timezone

System.out.println(sdf.format(calendar.getTime()));


查看完整回答
反對 回復(fù) 2024-01-28
  • 3 回答
  • 0 關(guān)注
  • 232 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號