【问题标题】:Date and calendar java日期和日历 java
【发布时间】:2010-12-02 04:58:38
【问题描述】:
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() 并设置日期。但我不知道如何实现它。请帮我了解如何使用日历功能设置日期

【问题讨论】:

标签: java


【解决方案1】:

tl;博士

LocalDate.of( 1986 , Month.FEBRUARY , 23 )

仅限日期

DateCalendar 这两个类都不适合。

您显然想要一个没有时间和时区的仅日期值。相比之下,Date 类是具有 UTC 时间的日期,Calendar 是具有时区的日期时间。

此外,DateCalendar 都已过时,由 java.time 类取代。

LocalDate

LocalDate 类表示没有时间和时区的仅日期值。

今天

时区对于确定日期至关重要。对于任何给定的时刻,日期在全球范围内因区域而异。例如,Paris France 中午夜过后几分钟是新的一天,而 Montréal Québec 中仍然是“昨天”。

如果没有指定时区,JVM 会隐式应用其当前的默认时区。该默认值可能随时更改,因此您的结果可能会有所不同。最好将所需/预期的时区明确指定为参数。

continent/region 的格式指定proper time zone name,例如America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用 3-4 个字母的缩写,例如 ESTIST,因为它们不是真正的时区,没有标准化,甚至不是唯一的 (!)。

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );  // Get current date for a particular time zone.

具体日期

或者指定一个日期。您可以通过一个数字来设置月份,1 月至 12 月的编号为 1-12,这与传统类中疯狂的从零开始的编号不同。

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 枚举对象,一年中的每个月一个。提示:在整个代码库中使用这些 Month 对象,而不是仅仅使用整数,以使您的代码更具自记录性、确保有效值并提供 type-safety

LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;

字符串

通过调用toString: YYYY-MM-DD 以标准ISO 8601 格式生成表示日期值的字符串。其他格式见DateTimeFormatter类。

String output = ld.toString() ;  // Generate a string in standard ISO 8601 format, YYYY-MM-DD.

关于java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

从哪里获得 java.time 类?

ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

【讨论】:

    【解决方案2】:
    String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
            "Sep", "Oct", "Nov", "Dec" };
    
    Calendar calendar = Calendar.getInstance();
    
    System.out.print("Date: ");
    System.out.print(months[calendar.get(Calendar.MONTH)]);
    System.out.print(" " + calendar.get(Calendar.DATE) + " ");
    System.out.println(calendar.get(Calendar.YEAR));
    
    System.out.print("Time: ");
    System.out.print(calendar.get(Calendar.HOUR) + ":");
    System.out.print(calendar.get(Calendar.MINUTE) + ":");
    System.out.println(calendar.get(Calendar.SECOND));
    
    calendar.set(Calendar.HOUR, 10);
    calendar.set(Calendar.MINUTE, 29);
    calendar.set(Calendar.SECOND, 22);
    
    System.out.print("Updated time: ");
    System.out.print(calendar.get(Calendar.HOUR) + ":");
    System.out.print(calendar.get(Calendar.MINUTE) + ":");
    System.out.println(calendar.get(Calendar.SECOND));
    

    【讨论】:

      猜你喜欢
      • 2010-10-02
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多