Java Day Add addDay(String currentdate, int add_day)

Here you can find the source of addDay(String currentdate, int add_day)

Description

add Day

License

Open Source License

Declaration

public static String addDay(String currentdate, int add_day) 

Method Source Code


//package com.java2s;
import java.text.*;
import java.util.*;

public class Main {
    public static String addDay(String currentdate, int add_day) {
        GregorianCalendar gc = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        int year, month, day;

        try {/*from ww  w . j ava2 s  .  c  o m*/
            year = Integer.parseInt(currentdate.substring(0, 4));
            month = Integer.parseInt(currentdate.substring(5, 7)) - 1;
            day = Integer.parseInt(currentdate.substring(8, 10));

            gc = new GregorianCalendar(year, month, day);
            gc.add(GregorianCalendar.DATE, add_day);

            return formatter.format(gc.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static int getTime(String sDateTime, String eDateTime) throws ParseException {
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date ssDateTime = myFormatter.parse(sDateTime);
        java.util.Date eeDateTime = myFormatter.parse(eDateTime);
        long l = (eeDateTime.getTime() - ssDateTime.getTime());
        long day = l / (24 * 60 * 60 * 1000);
        return (int) day;
    }
}

Related

  1. addDay(Date oriDate, int amount)
  2. addDay(int days)
  3. addDay(int k)
  4. addDay(java.util.Date date)
  5. addDay(java.util.Date dt, long day)
  6. addDay(String d)
  7. addDay(String data)
  8. addDay(String datetime, int day)
  9. addDayInterval(long millis, int dayInterval)