java - 以印度标准格式动态设置时区和返回时间

标签 java

我从后端接收到以下格式的 JSON 数组

[
    {
        "time": "4:40pm",
        "country": "Australia"
    },
    {
        "time": "3:30pm",
        "country": "america"
    },
    {
        "time": "6:30am",
        "country": "mexico"
    }
]

我需要解析这个 json 并将每次转换为 IST(印度)时间

我以这种方式开始,我正在设置时区,但无法将此时间转换为 IST

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class Testing {
    public static void main(String[] args) throws ParseException {
        String time = "4:40pm";
        String country = "Australia";
        convertDate(time, country);
    }

    public static String convertDate(String time, String country)
            throws ParseException {
        SimpleDateFormat in = new SimpleDateFormat("hh:mm");

        if (country.equals("Australia")) {
            in.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
            in.parse(time).toString();

        }
        return "";
    }
}

最佳答案

你就快到了。

如果您想打印适合IST的hh:mm格式,您可以:

if (country.equals("Australia")) {
    in.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
    SimpleDateFormat out = new SimpleDateFormat("hh:mm");
    out.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    return out.format(in.parse(time));
}

关于java - 以印度标准格式动态设置时区和返回时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33479565/

相关文章:

java - 如何让 android 只用一只耳朵发出哔哔声?

java - 如何随机执行一段代码并统计次数?

java - 为什么使用相同种子创建的两个 Random 对象从 hashcode() 产生不同的结果

java - Minecraft 模组逐渐改变玩家的方向

java - 列出 Android 中所有可用的通知铃声

java - 当焦点越过 Jtextfield 限制时将焦点移至下一个 JTextfield

java - MVC 和图形组件

java - 以编程方式触发 JTextField 中的按键事件?

java - java从网页中提取图像

java - AWS CloudWatch 自定义指标批处理