How to add days into the date in android

前端 未结 10 1107
眼角桃花
眼角桃花 2020-12-01 12:07

In my database I am getting start date like 2011-11-30(yyyy/mm/dd)format.and duration date like 40 days.How can i calculate the days and get new date format of mm/dd/yyyy.

相关标签:
10条回答
  • java.time

    The modern answer to this question is long overdue. Do consider using java.time, the modern Java date and time API, for your date work.

        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);
    

    Output is:

    01/09/2012

    Question: Doesn’t java.time require Android API level 26?

    java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

    • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
    • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
    • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

    Links

    • Oracle tutorial: Date Time explaining how to use java.time.
    • Java Specification Request (JSR) 310, where java.time was first described.
    • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
    • Java 8+ APIs available through desugaring
    • ThreeTenABP, Android edition of ThreeTen Backport
    • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
    0 讨论(0)
  • 2020-12-01 12:36
    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();
    
    0 讨论(0)
  • 2020-12-01 12:40

    This code is working 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);
    
    0 讨论(0)
  • 2020-12-01 12:41

    Just Simple copy this method pass data

    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;
        } 
    

    And call it like

     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);  
    

    You can put any format you want in your cituation

    ConvertToDate("2011-11-30","yyyy/mm/dd" ,"mm/dd/yyyy");  
    
    0 讨论(0)
提交回复
热议问题