簡體   English   中英

Android:如何獲取此處指定格式的時區字符串

[英]Android: how to get the timezone string in the format specified here

如何獲取時區格式並返回類似https://msdn.microsoft.com/zh-cn/library/gg154758.aspx的字符串- Time zone name ,例如"SA Pacific Standard Time"

或更糟糕的是,我怎么能這樣獲得: (UTC-05:00) Bogota, Lima, Quito ,或至少(UTC-05:00)

如果我將它們全部放在地圖中,以便可以將其手動匹配到以前的String?

你有沒有嘗試過?

TimeZone tz = TimeZone.getDefault();
System.out.println("TimeZone   "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezon id :: " +tz.getID());

結果是您的設備恰好在澳大利亞

時區GMT + 09:30時區ID ::澳大利亞/達爾文

這應該以字符串和日期對象的形式返回UTC。 您可以更改日期格式。

static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss"

public static Date getUTCdatetimeAsDate()
{
    //note: doesn't check for null
    return stringDateToDate(GetUTCdatetimeAsString());
}

public static String getUTCdatetimeAsString()
{
    final SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    final String utcTime = sdf.format(new Date());

    return utcTime;
}

public static Date stringDateToDate(String StrDate)
{
    Date dateToReturn = null;
    SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);

    try
    {
        dateToReturn = (Date)dateFormat.parse(StrDate);
    }
    catch (ParseException e)
    {
        e.printStackTrace();
    }

    return dateToReturn;
}

這可能無關緊要(不知道Android是否完全支持java-8),但是您可以使用標准java api來做到這一點:

ZonedDateTime dateTime = ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Asia/Tokyo]");
System.out.println(dateTime); // 2007-12-03T10:15:30+09:00[Asia/Tokyo]

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("(O) z");
System.out.println(dateTime.format(formatter)); // (GMT+9) JST

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM