如何更改我的代码,以便结果基于用户输入的年日?
我编写了下面的代码,根据“今天的日期”以及“提前 5 天”的日期计算“两年前”的日期。
我想制作一个动态版本的代码,遵循相同的比较原则。例如,我希望用户插入年数和天数并将其与今天的日期进行比较。
代码:
public class Calendar1{
private static void doCalendarTime() {
System.out.print("*************************************************");
Date now = Calendar.getInstance().getTime();
System.out.print(" \n Calendar.getInstance().getTime() : " + now);
System.out.println();
}
private static void doSimpleDateFormat() {
System.out.print("*************************************************");
System.out.print("\n\nSIMPLE DATE FORMAT\n");
System.out.print("*************************************************");
// Get today's date
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.print(" \n It is now : " + formatter.format(now.getTime()));
System.out.println();
}
private static void doAdd() {
System.out.println("ADD / SUBTRACT CALENDAR / DATEs");
System.out.println("=================================================================");
// Get today's date
Calendar now = Calendar.getInstance();
Calendar working;
SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
working = (Calendar) now.clone();
working.add(Calendar.DAY_OF_YEAR, - (365 * 2));
System.out.println (" Two years ago it was: " + formatter.format(working.getTime()));
working = (Calendar) now.clone();
working.add(Calendar.DAY_OF_YEAR, + 5);
System.out.println(" In five days it will be: " + formatter.format(working.getTime()));
System.out.println();
}
public static void main(String[] args) {
System.out.println();
doCalendarTime();
doSimpleDateFormat();
doAdd();
}
}
I have written the code below to calculate the date of "two years ago" based off the "today's date", as well as the date of "5 days ahead".
I want to make a dynamic version of the code, following the same comparison principle. For example, I want the user to insert the numbers of years and days and compare it to today's date.
Code:
public class Calendar1{
private static void doCalendarTime() {
System.out.print("*************************************************");
Date now = Calendar.getInstance().getTime();
System.out.print(" \n Calendar.getInstance().getTime() : " + now);
System.out.println();
}
private static void doSimpleDateFormat() {
System.out.print("*************************************************");
System.out.print("\n\nSIMPLE DATE FORMAT\n");
System.out.print("*************************************************");
// Get today's date
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.print(" \n It is now : " + formatter.format(now.getTime()));
System.out.println();
}
private static void doAdd() {
System.out.println("ADD / SUBTRACT CALENDAR / DATEs");
System.out.println("=================================================================");
// Get today's date
Calendar now = Calendar.getInstance();
Calendar working;
SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
working = (Calendar) now.clone();
working.add(Calendar.DAY_OF_YEAR, - (365 * 2));
System.out.println (" Two years ago it was: " + formatter.format(working.getTime()));
working = (Calendar) now.clone();
working.add(Calendar.DAY_OF_YEAR, + 5);
System.out.println(" In five days it will be: " + formatter.format(working.getTime()));
System.out.println();
}
public static void main(String[] args) {
System.out.println();
doCalendarTime();
doSimpleDateFormat();
doAdd();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Java 的标准日期时间 API 不太适合日期操作。如果你想对日期进行真正的计算,我建议使用像 Joda-Time 这样的库,它对于这种情况有更好的功能。
以下是与问题中使用的相同示例之后的 Joda-Time 代码:
这假设您想要使用系统的默认时区(子午线)。如果不是这种情况,则有一个构造函数的参数来设置将使用的时区。
Java's standard date-time APIs are not very good for operations on dates. If you want to do real calculations on dates, I suggest a library like Joda-Time, which has much better functionality for such cases.
Here is the a Joda-Time code following the same example used on the question:
This assumes you want to work with the system's default time-zone (meridian). If that is not the case, there is a constructor's argument to set the time-zone that will be used.