【问题标题】:How to add days into the date in android如何在android中的日期中添加天数
【发布时间】:2012-02-03 00:48:10
【问题描述】:

在我的数据库中,我得到像 2011-11-30(yyyy/mm/dd) 格式的开始日期和像 40 天这样的持续时间。我如何计算天数并获得 mm/dd/yyyy 的新日期格式.

谁能帮帮我

谢谢

【问题讨论】:

标签: android date


【解决方案1】:

java.time

这个问题的现代答案姗姗来迟。请考虑将现代 Java 日期和时间 API java.time 用于您的日期工作。

    DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM/dd/uuuu");
    int durationDays = 40;
    
    LocalDate startDate = LocalDate.parse("2011-11-30");
    LocalDate newDate = startDate.plusDays(durationDays);
    
    String formattedDate = newDate.format(outputFormatter);
    System.out.format(formattedDate);

输出是:

2012 年 1 月 9 日

问题:java.time 不需要 Android API 26 级吗?

java.time 在较旧和较新的 Android 设备上都能很好地工作。它只需要至少 Java 6

  • 在 Java 8 及更高版本以及更新的 Android 设备(从 API 级别 26 起)中,现代 API 是内置的。
  • 在非 Android Java 6 和 7 中,获取 ThreeTen Backport,这是现代类的后向端口(对于 JSR 310,ThreeTen;请参阅底部的链接)。
  • 在较旧的 Android 上,请使用脱糖或 Android 版本的 ThreeTen Backport。它被称为 ThreeTenABP。在后一种情况下,请确保使用子包从 org.threeten.bp 导入日期和时间类。

链接

【讨论】:

    【解决方案2】:

    不要使用:

    Calendar c = Calendar.getInstance();
    

    改用这个:

    GregorianCalendar calendar =new GregorianCalendar();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.DAY_OF_MONTH , 30);
    

    【讨论】:

    • “不使用”的原因是什么,您是否遇到过错误或什么?!
    【解决方案3】:

    这段代码应该能很好地完成这项工作!!!

    public static String addDay(String oldDate, int numberOfDays) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(dateFormat.parse(oldDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.add(Calendar.DAY_OF_YEAR,numberOfDays);
        dateFormat=new SimpleDateFormat("MM-dd-YYYY");
        Date newDate=new Date(c.getTimeInMillis());
        String resultDate=dateFormat.format(newDate);
        return resultDate;
    }
    

    更多功能请查看此链接
    Add days,months,years to a date

    【讨论】:

    • oldDate in c.setTime(dateFormat.parse(oldDate)); 是一个日期,函数需要一个日期。
    【解决方案4】:

    它对我有用。

    Calendar c = Calendar.getInstance();
    int mYear = c.get(Calendar.YEAR);
    int mMonth = c.get(Calendar.MONTH);
    int mDay = c.get(Calendar.DAY_OF_MONTH);
    
    String CurrentDate = mYear + "/" + mMonth + "/" + mDay;
    String dateInString = dob; // Select date
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    c = Calendar.getInstance();
    try {
    c.setTime(sdf.parse(dateInString));
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    c.add(Calendar.DATE, 14);    //14 dats add
    sdf = new SimpleDateFormat("dd/MM/yyyy");
    Date resultdate = new Date(c.getTimeInMillis());
    dateInString = sdf.format(resultdate);
    System.out.println(dateInString);
    

    【讨论】:

      【解决方案5】:

      只需简单复制此方法传递数据

      public static String ConvertToDate(String dateString,String inputFormater ,String outputFormater) {
              String outputText = "" ;
              try {
                  SimpleDateFormat inputFormat = new SimpleDateFormat(inputFormater);
                  SimpleDateFormat outputFormat = new SimpleDateFormat(outputFormater);
                  Date parsed = null;
                  parsed = inputFormat.parse(dateString);
                  outputText = outputFormat.format(parsed);
      
              Log.d(TAG, "  msg : " + outputText);
              } catch (ParseException e) {
                  e.printStackTrace();
                  Log.e(TAG, "ERROR : " + e.getMessage());
              }
              return outputText;
          } 
      

      然后这样称呼它

       public static String INPUTE_JSON_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
       public static String FORMAT_FOR_JOB_APPLIED = "MMM dd, yyyy";
       String date ="2017-10-29";
       ConvertToDate(date ,DateUtils.INPUTE_JSON_DATE_FORMAT ,DateUtils.FORMAT_FOR_JOB_APPLIED);  
      

      你可以在你的引文中输入任何你想要的格式

      ConvertToDate("2011-11-30","yyyy/mm/dd" ,"mm/dd/yyyy");  
      

      【讨论】:

      • 问题是如何给日期加上天数。
      【解决方案6】:

      此代码 100% 工作

                  Calendar c = Calendar.getInstance();
                  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss");
                  String reg_date = df.format(c.getTime());
                  showtoast("Currrent Date Time : "+reg_date);
      
                  c.add(Calendar.DATE, 3);  // number of days to add
                  String end_date = df.format(c.getTime());
                  showtoast("end Time : "+end_date);
      

      【讨论】:

        【解决方案7】:

        你可以试试这样的,

        String dt = "2012-01-04";  // Start date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(sdf.parse(dt));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.add(Calendar.DATE, 40);  // number of days to add, can also use Calendar.DAY_OF_MONTH in place of Calendar.DATE
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd-yyyy");
        String output = sdf1.format(c.getTime()); 
        

        【讨论】:

        • 附注,Calendar.DATE 是 Calendar.DAY_OF_MONTH 的同义词,可能更具可读性
        • @Styler 在评论中添加。谢谢。
        • 虽然这在 2012 年是一个很好的答案,但我建议我们不再使用 SimpleDateFormatCalendar。这些类设计不良且过时,尤其是前者,尤其是出了名的麻烦。而是使用来自java.time, the modern Java date and time APILocalDateDateTimeFormatter
        【解决方案8】:
        Date dtStartDate=o.getStartDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Calendar c = Calendar.getInstance();
        c.setTime(dtStartDate);
        c.add(Calendar.DATE, 3);  // number of days to add
        String dt = sdf.format(c.getTime());  // dt is now the new date
        Toast.makeText(this, "" + dt, 5000).show();
        

        【讨论】:

        • 这个例子增加了 3 天。如果要 3 天前,请将参数更改为 -3。
        【解决方案9】:
        Calendar c = Calendar.getInstance();
            int mYear = c.get(Calendar.YEAR);
            int mMonth = c.get(Calendar.MONTH);
            int mDay = c.get(Calendar.DAY_OF_MONTH);
        
            // display the current date
            String CurrentDate = mYear + "/" + mMonth + "/" + mDay;
        
            String dateInString = CurrentDate; // Start date
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        
            c = Calendar.getInstance();
        
            try {
                c.setTime(sdf.parse(dateInString));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        
            c.add(Calendar.DATE, 7656);//insert the number of days you want to be added to the current date
            sdf = new SimpleDateFormat("dd/MM/yyyy");
            Date resultdate = new Date(c.getTimeInMillis());
            dateInString = sdf.format(resultdate);
        
            //Display the Result in the Edit Text or Text View your Choice
            EditText etDOR = (EditText)findViewById(R.id.etDateOfReturn);
            etDOR.setText(dateInString);
        

        【讨论】:

          【解决方案10】:

          Step-1 从指定字符串中获取日历实例

          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                      Calendar c = Calendar.getInstance();
                      c.setTime(sdf.parse(dateInString));
          

          第二步使用add()向日历添加天数

          c.add(Calendar.DATE, 40); 
          

          步骤 3 将 dtae 转换为生成的日期格式

          sdf = new SimpleDateFormat("MM/dd/yyyy");
                      Date resultdate = new Date(c.getTimeInMillis());
                      dateInString = sdf.format(resultdate);
          

          源代码

          String dateInString = "2011-11-30";  // Start date
                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                  Calendar c = Calendar.getInstance();
                  c.setTime(sdf.parse(dateInString));
                  c.add(Calendar.DATE, 40);  
                  sdf = new SimpleDateFormat("MM/dd/yyyy");
                  Date resultdate = new Date(c.getTimeInMillis());
                  dateInString = sdf.format(resultdate);
                  System.out.println("String date:"+dateInString);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-10
            • 1970-01-01
            • 2011-09-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多