提问人:Prakash 提问时间:8/9/2023 最后编辑:AnonymousPrakash 更新时间:8/11/2023 访问量:56
将服务器 GMT 日期和时间转换为具有指定格式的用户指定的日期和时区,并返回 Date 数据类型 [duplicate]
Converting Server GMT date and time to User specified date and time zone with specified format, returning with Date datatype [duplicate]
问:
我正在尝试将从服务器收到的日期转换为用户特定的日期格式 (IST) 并返回为 Date 数据类型。附上示例代码和结果,如下所示
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class ServerToClientDateTime {
public static void main(String[] args) {
try {
String clientSpecificFormat = "yyyy-MM-dd HH:mm:ss";
String clientTimezone1 = "America/New_York";
String clientTimezone2 = "Asia/Kolkata";
Date date = new Date();
System.out.println(new Date());
SimpleDateFormat dateFormat = new g(clientSpecificFormat);
TimeZone americaNewyork = TimeZone.getTimeZone(clientTimezone1);
TimeZone asiaKolkata = TimeZone.getTimeZone(clientTimezone2);
dateFormat.setTimeZone(americaNewyork);
System.out.println("\namerica/ newyork:" + dateFormat.format(date));
System.out.println("Date format :" + dateFormat.parse(dateFormat.format(date)));
dateFormat.setTimeZone(asiaKolkata);
System.out.println("\nAsia/ Kolkata:" + dateFormat.format(date));
System.out.println("Date format :" + dateFormat.parse(dateFormat.format(date)));
} catch (Exception exp) {
System.out.println(exp);
}
}
}
程序的结果为
Wed Aug 09 11:57:46 IST 2023
america/ newyork:2023-08-09 02:27:46
Date format :Wed Aug 09 11:57:46 IST 2023
Asia/ Kolkata:2023-08-09 11:57:46
Date format :Wed Aug 09 11:57:46 IST 2023
为什么美国/New_York解析后返回与格林威治标准时间相同,如何获取预期的 IST 日期格式。
谢谢
答: 暂无答案
评论
Date
Instant.now()
java.util.Date
String
Date
Date