日期和日历java [英] Date and calendar java
问题描述
class Employee
{
private Date doj;
public Employee (Date doj)
{
this.doj=doj;
}
public Date getDoj()
{
return doj;
}
}
class TestEmployeeSort
{
public static List<Employee> getEmployees()
{
List<Employee> col=new ArrayList<Employee>();
col.add(new Employee(new Date(1986,21,22));
}
}
在上面的代码中,我使用Date来设置日期。我想知道如何使用日历功能来执行此操作。我知道我可以使用getInstance()并设置日期但我不知道如何实现它。请帮助我知道如何使用日历函数设置日期
In the above code i have used Date to set a date. I want to know how to use calendar function to do this. I know that i can use getInstance() and set the date. But I don't know how to implement it. Please help me to know how to set Date using Calendar function
推荐答案
tl ;博士
tl;dr
LocalDate.of( 1986 , Month.FEBRUARY , 23 )
仅限日期
这两个类都没有,日期
& 日历
,是合适的。
Date-only
Neither of those classes, Date
& Calendar
, are suitable.
你显然想要一个没有时间的仅限日期的值相比之下,日期
类是一个以UTC为单位的日期和日历的日期
是带时区的日期时间。
You apparently want a date-only value without a time-of-day and without a time zone. In contrast, the Date
class is a date with a time-of-day in UTC, and Calendar
is a date-time with a time zone.
此外,日期
& ; 日历
已过时,由 j取代ava.time 类。
Furthermore, both Date
& Calendar
are obsolete, replaced by the java.time classes.
LocalDate
class表示没有时间且没有时区的仅限日期的值。
The LocalDate
class represents a date-only value without time-of-day and without time zone.
时区对于确定日期至关重要。对于任何给定的时刻,日期在全球范围内因地区而异。例如,在巴黎法国午夜后的几分钟是新的一天,而昨天在蒙特利尔魁北克。
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still "yesterday" in Montréal Québec.
如果未指定时区,则JVM会隐式应用其当前的默认时区。该默认值可能随时更改,因此您的结果可能会有所不同。最好明确指定您期望/预期的时区作为参数。
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.
指定正确的时区名称,格式为 continent / region
,例如 America / Montreal
, 非洲/卡萨布兰卡
,或太平洋/奥克兰
。切勿使用3-4字母缩写,例如 EST
或 IST
,因为它们不真正的时区,非标准化,甚至不是唯一的(!)。
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z ); // Get current date for a particular time zone.
具体日期
或指定日期。您可以将月份设置为一个数字,1月至12月的数字为1-12,与遗留类中疯狂的从零编号不同。
Specific date
Or specify a date. You may set the month by a number, with sane numbering 1-12 for January-December, unlike the crazy zero-based numbering in the legacy class.
LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ; // Both year and month have same numbering. 1986 is the year 1986. 1-12 is January-December.
或者,更好的是,使用 月
枚举预先定义的对象,每个对象一个一年中的一个月。提示:在整个代码库中使用这些 Month
对象,而不仅仅是整数,以使代码更加自我记录,确保有效值,并提供类型安全。
Or, better, use the Month
enum objects pre-defined, one for each month of the year. Tip: Use these Month
objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety.
LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;
字符串
生成表示字符串的字符串标准 ISO 8601 格式的日期值,方法是调用 toString
:YYYY-MM-DD。有关其他格式,请参阅 DateTimeFormatter
class。
Strings
Generate a String representing the date value in standard ISO 8601 format by calling toString
: YYYY-MM-DD. For other formats, see DateTimeFormatter
class.
String output = ld.toString() ; // Generate a string in standard ISO 8601 format, YYYY-MM-DD.
关于java.time
java.time 框架内置于Java 8及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如 java.util.Date
, 日历
,& SimpleDateFormat
。
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
Joda-Time 项目,现在在维护模式,建议迁移到 java.time 类。
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
要了解更多信息,请参阅 Oracle教程。并搜索Stack Overflow以获取许多示例和解释。规范是 JSR 310 。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
从哪里获取java.time类?
Where to obtain the java.time classes?
- Java SE 8 , Java SE 9 ,以及之后
- 内置。
- 带有捆绑实现的标准Java API的一部分。
- Java 9添加了一些小功能和修复。
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- 大部分java.time功能都被反向移植到Java 6& 7 ThreeTen-Backport 。
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- 更新版本的Android捆绑实现java.time类。
- 对于早期的Android, ThreeTenABP 项目适应 ThreeTen-Backport (如上所述)。请参阅 如何使用ThreeTenABP ...... 。
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
ThreeTen-Extra 项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在这里找到一些有用的课程,例如
Interval
,YearWeek
,YearQuarter
,更多。这篇关于日期和日历java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!