如何更改我的代码,以便结果基于用户输入的年日?

发布于 2024-10-09 21:20:48 字数 1978 浏览 0 评论 0原文

我编写了下面的代码,根据“今天的日期”以及“提前 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时模样 2024-10-16 21:20:48

Java 的标准日期时间 API 不太适合日期操作。如果你想对日期进行真正的计算,我建议使用像 Joda-Time 这样的库,它对于这种情况有更好的功能。


以下是与问题中使用的相同示例之后的 Joda-Time 代码:

DateTime now = new DateTime();
DateTime twoYearsAgo = now.minusYears(2);
DateTime fiveDaysFromNow = now.plusDays(5);

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
   .appendPattern("E yyyy.MM.dd 'at' hh:mm:ss a zzz")
   .toFormatter();

System.out.println(formatter.print(twoYearsAgo));
System.out.println(formatter.print(fiveDaysFromNow));

这假设您想要使用系统的默认时区(子午线)。如果不是这种情况,则有一个构造函数的参数来设置将使用的时区。

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:

DateTime now = new DateTime();
DateTime twoYearsAgo = now.minusYears(2);
DateTime fiveDaysFromNow = now.plusDays(5);

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
   .appendPattern("E yyyy.MM.dd 'at' hh:mm:ss a zzz")
   .toFormatter();

System.out.println(formatter.print(twoYearsAgo));
System.out.println(formatter.print(fiveDaysFromNow));

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文