java java获取当前计算机的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11283264/
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
java get the current computer's date
提问by Totti
how can i get computer's date on java, i want just year, month, day i tried to get calender like this
我如何在 Java 上获取计算机的日期,我只想要年、月、日我试图像这样获取日历
Calendar c =Calendar.getInstance();
c.clear(Calendar.HOUR)
but can't know to deal with it,
但不知道如何处理,
回答by 11684
First link on google:
谷歌上的第一个链接:
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
private String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
return dateFormat.format(date);
}
回答by Kumar Vivek Mitra
Try this ONE line of code.....
试试这一行代码.....
// Prints 01-07-2012
// 打印 01-07-2012
System.out.println(new SimpleDateFormat("dd-MM-YYYY").format(new Date()));
// Prints 01-Jul-2012
// 打印 01-Jul-2012
System.out.println(new SimpleDateFormat("dd-MMM-YYYY").format(new Date()));
回答by óscar López
You can obtain the current date as a year/month/day
string like this:
您可以year/month/day
像这样获取当前日期作为字符串:
String strDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
System.out.println(strDate);
> 2012-07-01
Notice that a Date
object will alwayscontain year, month, day, hours, minutes, seconds, milliseconds, etc., and by calling new Date()
you obtain a date object with the current time.
请注意,Date
对象将始终包含年、月、日、小时、分钟、秒、毫秒等,通过调用new Date()
您可以获得具有当前时间的日期对象。
If you only need some fields of a date (say, year, month and day) you need to format the date using a formatter, for instance SimpleDateFormat. Check the link for learning more about the string formatting options available in Java.
如果您只需要日期的某些字段(例如,年、月和日),您需要使用格式化程序格式化日期,例如SimpleDateFormat。查看链接以了解有关 Java 中可用字符串格式选项的更多信息。
回答by Michael Besteck
Calendar rightNow = Calendar.getInstance();
int y = rightNow.get(Calendar.YEAR);
int m = rightNow.get(Calendar.MONTH) + 1;
int d = rightNow.get(Calendar.DAY_OF_MONTH);
System.out.println("year "+y+" month "+m+" day "+d);
回答by Basil Bourque
Joda-Time
乔达时间
Some example code using the Joda-Time2.4 library.
使用Joda-Time2.4 库的一些示例代码。
The java.util.Date and .Calendar classes are notoriously troublesome. Avoid them. Use either Joda-Time or the new java.time package in Java 8 (inspired by Joda-Time, defined by JSR 310).
java.util.Date 和 .Calendar 类是出了名的麻烦。避开它们。使用 Joda-Time 或 Java 8 中的新 java.time 包(受 JSR 310 定义的 Joda-Time 启发)。
Time Zone
时区
Note the use of a time zone. A time zone is necessary to determine a date. The same simultaneous moment in Kolkata and Paris may have different dates on the calendar. If you omit a time zone, the JVM's current default time zone will be applied. That means your results may vary, so best to explicitly specify the time zone you intend.
请注意时区的使用。需要时区来确定日期。加尔各答和巴黎的同一时刻在日历上可能有不同的日期。如果省略时区,则将应用 JVM 当前的默认时区。这意味着您的结果可能会有所不同,因此最好明确指定您想要的时区。
Example Code
示例代码
String output = LocalDate.now( TimeZone.forID( "Asia/Kolkata" ) ).toString();