如何使用 Java 将当前 UTC 时区转换为印度标准时间

我正在尝试使用以下代码转换当前的 UTC 时间(来自我的 Linux 服务器的时间)。


import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.TimeZone;


public class UtcToIst {


    public static void main(String[] args) {

        List<String> timeZones = new ArrayList<String>();

        String ISTDateString = "";

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

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

        String utcTime = sdf.format(new Date());

        System.err.println("utcTime: " + utcTime);

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

        String pattern = "dd-MM-yyyy HH:mm:ss";

        SimpleDateFormat formatter;

        formatter = new SimpleDateFormat(pattern);

        try {

            String formattedDate = formatter.format(utcTime);

            Date ISTDate = sdf.parse(formattedDate);

            ISTDateString = formatter.format(ISTDate);

            timeZones.add(utcTime+ ","+ ISTDateString);

        } catch(Exception e) {

            e.printStackTrace();

        }


        for(String i: timeZones) {

            System.out.println(i);

        }

    }

}

当我执行代码时,出现以下异常:


utcTime: 05-11-2018 12:55:28

java.lang.IllegalArgumentException: Cannot format given Object as a Date

    at java.text.DateFormat.format(DateFormat.java:310)

    at java.text.Format.format(Format.java:157)

    at UtcToIst.main(UtcToIst.java:21)

我看到 UTC 时间被正确提取为:05-11-2018 12:55:28 但代码无法将字符串解析为 IST(印度标准时间)。我无法理解如何解决问题。谁能让我知道我在这里犯了什么错误,我该如何解决?


叮当猫咪
浏览 360回答 3
3回答

慕虎7371278

此行无用并导致错误(utcTime 不是日期,而是字符串)。&nbsp; &nbsp; &nbsp; &nbsp;String formattedDate = formatter.format(utcTime);只需更换:&nbsp; &nbsp; &nbsp; &nbsp; String formattedDate = formatter.format(utcTime);&nbsp; &nbsp; &nbsp; &nbsp; Date ISTDate = sdf.parse(formattedDate);和:&nbsp; &nbsp; &nbsp; &nbsp; Date ISTDate = sdf.parse(utcTime);全班:import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.TimeZone;public class UtcToIst {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; List<String> timeZones = new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; String ISTDateString = "";&nbsp; &nbsp; &nbsp; &nbsp; SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");&nbsp; &nbsp; &nbsp; &nbsp; sdf.setTimeZone(TimeZone.getTimeZone("UTC"));&nbsp; &nbsp; &nbsp; &nbsp; String utcTime = sdf.format(new Date());&nbsp; &nbsp; &nbsp; &nbsp; System.err.println("utcTime: " + utcTime);&nbsp; &nbsp; &nbsp; &nbsp; sdf.setTimeZone(TimeZone.getTimeZone("UTC"));&nbsp; &nbsp; &nbsp; &nbsp; String pattern = "dd-MM-yyyy HH:mm:ss";&nbsp; &nbsp; &nbsp; &nbsp; SimpleDateFormat formatter;&nbsp; &nbsp; &nbsp; &nbsp; formatter = new SimpleDateFormat(pattern);&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Date ISTDate = sdf.parse(utcTime);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ISTDateString = formatter.format(ISTDate);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timeZones.add(utcTime+ ","+ ISTDateString);&nbsp; &nbsp; &nbsp; &nbsp; } catch(Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for(String i: timeZones) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

繁华开满天机

改用 LocalDateTimeDateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");LocalDateTime now = LocalDateTime.now();System.out.println(dtf.format(now));

一只甜甜圈

注意:如果服务器时区不是印度时区,@Benoit 的回答会产生错误的时间结果,应明确设置时区为印度。第一:在SimpleDateFormat, Asia/Kolkata 上为印度时间设置正确的时区。第二:使用utc formatter解析格式化的时间字符串,得到utc时间实例。最后: 使用 Indian 格式化 utc 时间实例。见下面的代码:SimpleDateFormat utcFormatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));String utcTime = utcFormatter.format(new Date());System.err.println("utcTime: " + utcTime);Date utcTimeInstance = utcFormatter.parse(utcTime);SimpleDateFormat indianFormatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");indianFormatter.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));String indianTime = indianFormatter.format(utcTimeInstance);System.err.println("indianTime: " + indianTime);在我的电脑上打印:utcTime: 05-11-2018 13:42:31indianTime: 05-11-2018 19:12:31
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java