java 如何在java中获取今天的日期(yyyy-mm-dd)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32026013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get today's date in java(yyyy-mm-dd)
提问by Saad
how could I get today's date (current system date) on format yyyy-mm-dd (just date and not dateTime) to compare with some other dates of the same format.
我怎样才能以 yyyy-mm-dd(只是日期而不是日期时间)格式获取今天的日期(当前系统日期)以与其他一些相同格式的日期进行比较。
回答by Mureinik
You can use a SimpleDateFormat
to format a new Date()
:
您可以使用 aSimpleDateFormat
来格式化 a new Date()
:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String formatted = df.format(new Date());
回答by Goyal Vicky
Try this:-
试试这个:-
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
String formatted = format1.format(cal.getTime());
System.out.println(formatted);
}
回答by DalekCaan99
Try creating a variable in this format.
尝试以这种格式创建一个变量。
LocalDateTime date = LocalDateTime.now();
System.out.println( date );