DateTime转换为本地时间的问题

时间:2018-06-02 09:56:37

标签: java android datetime timezone

我在Android中遇到问题,DateTime转换为带有时区的本地时间。我使用以下方法转换为当地时间,但我得到的是'年份'和'时间'错误。

PS 收到的日期时间是孟加拉国标准时间"亚洲/达卡","(GMT +06:00)达卡"

public String localToGMT(String serverDate) {
    String DATE_FORMAT = "dd.mm.yyyy HH:MM";//15.06.18 6:00
    String strDate = "";
    ///Date date = new Date();
    try {
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date gmt = null;

        gmt = sdf.parse(serverDate);
        strDate = sdf.format(gmt);

    } catch (ParseException e) {
        e.printStackTrace();
    }
    return strDate;
}

输入:22.06.2018 00:00

我的输出:22.06.2017 00:12

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:1)

MM代表月份,mm代表minutes,您需要重新设置时区,试试这个:

public static void main(String[] args) throws Exception {
    String serverDate = "22.06.2018 16:00";
    String DATE_FORMAT = "dd.MM.yyyy HH:mm";
    String strDate;

    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    sdf.setTimeZone(TimeZone.getTimeZone("Asia/Dhaka"));
    Date gmt = sdf.parse(serverDate);

    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    strDate = sdf.format(gmt);
    System.out.println(strDate); // 22.06.2018 10:00
}
相关问题