gpt4 book ai didi

java - 如何使用字符串 fromTZ 日期从时区 'fromTZ' 转换为 'toTZ'?

转载 作者:行者123 更新时间:2023-12-01 15:21:18 26 4
gpt4 key购买 nike

如何将日期字符串转换为长字符串?我希望方法签名为 public static long Convert2(String dateStr,TimeZone fromTz, TimeZone toTz) ,其中 dateStr 的格式为 dd/MM/yyyy.

最佳答案

public static long convert2(String dateStr,TimeZone fromTz, TimeZone toTz)
// Format in which you are getting the date
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
// Set the formater to the timezone in which you are getting the date
formatter.setTimeZone(fromTz);

// Converting the string date to date
Date date = formatter.parse(dateStr);

// Prints the date in the from time zone timezone. Not required as per the quest. Just for info
System.out.println(formatter.format(date));

// Set the formatter to use a different timezone
formatter.setTimeZone(toTz);

// Prints the date in the to time zone timezone. Not required as per the quest. Just for info
System.out.println(formatter.format(date));

// converting the new Timzone date to long as that is what is required
long longDate = date.getTime();

// return the long date.
return longDate;

关于java - 如何使用字符串 fromTZ 日期从时区 'fromTZ' 转换为 'toTZ'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10878027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com