我有一个reactjs前端应用程序。它选择日期并发送到java后台。在java中,我解析日期的代码是:输入日期为Mon Aug 18 2014 18:11:54 GMT+0800 (Singapore Standard Time)。
我的日期格式应该是什么?
String inputdate = "Mon Aug 18 2014 18:11:54 GMT+0800 (Singapore Standard Time)";
DateFormat dateformat = new SimpleDateFormat("E MMM yy hh:mm:ss (z)");
try {
Date r=dateformat.parse(inputdate);
} catch (ParseException e) {
e.printStackTrace();
}发布于 2019-12-15 23:31:15
您需要将DateFormatter pattern更改为以下模式:
String inputdate = "Mon Aug 18 2014 18:11:54 GMT+0800 (Singapore Standard Time)";
DateFormat dateformat = new SimpleDateFormat("EE MMM d y H:m:s 'GMT'z");
try {
Date date=dateformat.parse(inputdate);
System.out.println(date.toString());
} catch (ParseException e) {
e.printStackTrace();
}但是,我的建议是使用将日期转换为毫秒,并将其发送到后端。
JS/React
var dateInMilliSecs = new Date().getTime(); // Let's say dateInMilliSecs=1576423777189Java
long dateInMilliSecs = 1576423777189L;
Date date = new Date(dateInMilliSecs);
String formatted = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);https://stackoverflow.com/questions/59345183
复制相似问题