【问题标题】:write a program to prompt number of days to add to current date and print new date as well as today's date [duplicate]编写一个程序来提示将天数添加到当前日期并打印新日期以及今天的日期[重复]
【发布时间】:2015-09-24 05:00:56
【问题描述】:

打印出今天的日期。这应该是 MM/DD/YYYY 的形式。月份应该从 1 而不是 0 开始。提示读取要添加到当前日期的天数并打印新日期。

请大家帮帮我

    import java.util.calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;

    public class JavaDateAdd {

     public static void main(String[] args) {
    Date date = new Date();

    System.out.println("Today's Date Is: " + (now.get(Calendar.MONTH) + 1) +         "/" + now.get(Calendar.DATE) + "/" + now.get(Calendar.YEAR));

    System.out.print("Number of Days You Want To ADD: ");
    int AddDays = in.nextInt();

    Date newDate = addDays(date,AddDays);
        System.out.println("Java Date after adding "+AddDays+" days: "+(now.get(Calendar.MONTH) + 1) + "/" + now.get(Calendar.DATE) + "/" + now.get(Calendar.YEAR));


     }
    }`

【问题讨论】:

标签: java calendar add gregorian-calendar days


【解决方案1】:

您可以修改代码以接受不占用天数并接受您的日期格式。

public static void main(String[] args) {

               Calendar cal = Calendar.getInstance();
               // print current date
               System.out.println("The current date is : " + cal.getTime());
               // add 20 days to the calendar
               cal.add(Calendar.DATE, 20);
               System.out.println("20 days later: " + cal.getTime());
               Date tommrrow = cal.getTime();
               SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy");
               String date = formatter.format(tommrrow);
               System.out.println("20 days in dd-MM-yy: " + date);

}

【讨论】:

    【解决方案2】:

    使用 SimpleDateFormat 格式化日期: SimpleDateFormat 是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。 SimpleDateFormat 允许您从选择任何用户定义的日期时间格式模式开始。例如:

    import java.util.*;
    import java.text.*;
    
    public class HelloWorld {
    public static void main(String args[]) {
    
        Date dNow = new Date( );
        SimpleDateFormat sdf = 
        new SimpleDateFormat ("MM/dd/yyyy ");
        System.out.println("Current Date: " + sdf.format(dNow));
    
        Calendar c = Calendar.getInstance();
        c.setTime(new Date()); // Now use today date.
        c.add(Calendar.DATE, 35); // Adding 35 days
        String output = sdf.format(c.getTime());
        System.out.println("New Date: "+ output);
     }
    }
    

    这将产生以下结果:

    Current Date: 09/24/2015                                                                                                                               
    New Date: 10/29/2015 
    

    【讨论】:

      【解决方案3】:

      打印出今天的日期。这应该是 MM/DD/YYYY 的形式。月份应该从 1 而不是 0 开始。

      首先查看DateTimeFormatter,例如:

      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
      System.out.println("Today is " + formatter.format(LocalDateTime.now()));
      

      提示读取要添加到当前日期的天数

      首先查看Scanning,例如:

          Scanner scanner = new Scanner(System.in);
          System.out.print("How many days to add: ");
          int days = scanner.nextInt();
      

      并打印新日期。

      首先查看Java 8's Date Time API,例如:

      LocalDateTime ldt = LocalDateTime.now();
      ldt = ldt.plusDays(days);
      System.out.println(formatter.format(ldt));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-29
        • 2021-06-16
        • 1970-01-01
        • 1970-01-01
        • 2023-02-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多