【问题标题】:JAVA how to take a Date and see the day, month, and year? [duplicate]JAVA如何取一个日期并查看年月日? [复制]
【发布时间】:2015-02-08 06:18:25
【问题描述】:

我有一个班级的作业,我们参加一个日期,然后看看给定的日期是否有效。如果是,我们就在上面加一天。我遇到问题的地方是在我检查日期是否有效后将其保存到

Date appointment = new Date();

appointment = new Date(month, day, year);
Date.advanceDate(appointment);

在另一个名为 Date.java 的文件中

public static void advanceDate(Date aDate){
    //here is where I need to read the date in aDate
}

通过Java api在线搜索后,我无法找到将日期添加到约会或从约会中获取日期、月份和年份的方法,然后将其保存为新日期

我在查看http://docs.oracle.com/javase/8/docs/api/java/util/Date.html之后所做的尝试

aDate.getDay();

但是 eclipse 告诉我“方法 getDate() 没有为数据类型定义

aDate.toString();

这不会返回月份日期和年份,而是返回其在内存中的位置

我在网上找到的每个解决方案都使用 Calender,它似乎已经取代了 Date

【问题讨论】:

标签: java


【解决方案1】:

我相信您正在寻找CalendarDateFormat

int year = 2015;
int month = Calendar.FEBRUARY; // <-- this is actually a 1.
int date = 8;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.set(year, month, date);
cal.add(Calendar.DAY_OF_MONTH, 1);
System.out.println(df.format(cal.getTime()));

输出是

2015-02-09

1FEBRUARY.

【讨论】:

    猜你喜欢
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    相关资源
    最近更新 更多