java 我正在使用 selenium webdriver 并想设置当前日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36595283/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:38:53  来源:igfitidea点击:

I am working on selenium webdriver and want to set the current date

javaseleniumselenium-webdriver

提问by pravardh raguel

I tried doing this:

我试过这样做:

Date date = new Date();
String lastDate = (date.toString().trim());
// display time and date using toString()
System.out.println(date.toString());

but here the system date is being displayed as:

但在这里系统日期显示为:

"Wed Apr 13 15:39:50 IST 2016"

and all I need is "MM/dd/yyyy"

我只需要“MM/dd/yyyy”

回答by Eduardo Yá?ez Parareda

You need to format it:

您需要对其进行格式化:

DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

//get current date time with Date()
Date date = new Date();

// Now format the date
String dateFormatted= dateFormat.format(date);

回答by anand

DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

//get current date time of system with Date()

//获取系统的当前日期时间 Date()

Date date = new Date();

String date1= dateFormat.format(date);  // Now format the date

// Print the Date
System.out.println("Current date and time is " +date1);

for detail: http://www.alltestingstuff.com/

详情:http: //www.alltestingstuff.com/

回答by Kovacic

Here is an example:

下面是一个例子:

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(dateFormat.format(new Date()));