JS 取得当前日期格式为 xxxx-xx-xx

比如:2012-01-01
2025-04-12 01:53:00
推荐回答(2个)
回答1:

1,Date date = new Date();

System.out.println(date.toLocaleString());

2,以上程序是使用上述格式打印出当前的日期。

3,以下是根据上述格式的字符串构造出一个date对象供程序使用

public static Date constructDate(String time){

boolean result = Pattern.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}", time);

if(!result)

return null;

String ymd = time.split(" ")[0];
String hms = time.split(" ")[1];
String[] ymds = ymd.split("-");
String[] hmss = hms.split(":");
return new Date(Integer.parseInt(ymds[0])-1900,Integer.parseInt(ymds[1])-1,Integer.parseInt(ymds[2]),
Integer.parseInt(hmss[0]),Integer.parseInt(hmss[1]),Integer.parseInt(hmss[2]));
}

回答2:

懒得回答,太简单了。
提示:去查js Date类。
var d = new Date();
var year = d.getFullYear();
// ......这样写下去吧。