Get Date based on day count in Java

后端 未结 7 1162
感情败类
感情败类 2021-01-13 21:46

Simple Question, but Google surprisingly had little on this. I have the number of days from Jan 1st of the year. How can I convert that to a date i

7条回答
  •  梦毁少年i
    2021-01-13 22:13

    Using GregorianCalendar and simpleDateFormat:

    int days = 20;
    GregorianCalendar dayCountofYear = new GregorianCalendar(2020,Calendar.JANUARY, 1);
    dayCountofYear.add(Calendar.DAY_OF_YEAR, days);
    System.out.println("GregorianCalendar: "+ dateFormat.format(dayCountofYear.getTime()));
    

    Above logic can be simplified in a function call as below:

    Date nextDate = addDaysToDate("01/01/2020", 20);
    System.out.println("After Adding Number of Days:"+nextDate); // O/P:21/01/2020
    

    Full Example:

    public class DateHelper {
    
        public static void main(String[] args) throws ParseException {
            int days = 20;
            String initialDay = "01/01/2020";
            Date nextDate = addDaysToDate(initialDay, days);
            System.out.println("After Adding Number of Days:"+nextDate);
    
            /*Date firstDate = DateHelper.parseDate(initialDay, formatStr);
            int daysBetween = getDaysBetween(firstDate, nextDate);
            System.out.println(days + " - Days Between - "+daysBetween);*/
        }
    
        static String formatStr = "dd/MM/yyyy";
        static DateFormat dateFormat = new SimpleDateFormat(formatStr);
        static DateFormat reverseDateFormat = new SimpleDateFormat("yyyy/MM/dd");
    
        public static Date addDaysToDate(String specificDate, int daysCount) throws ParseException {
            Date date = dateFormat.parse(specificDate);
            GregorianCalendar gregCal = (GregorianCalendar) GregorianCalendar.getInstance(); // Current Date
            gregCal.setTime(date); // Change the Date to Provided One
            gregCal.add(Calendar.DAY_OF_YEAR, daysCount);
            Date time = gregCal.getTime();
    
            ZonedDateTime zdt = gregCal.toZonedDateTime();
            System.out.println("GregorianCalendar DayofWeek: "+ zdt.getDayOfWeek());
            return time;
        }
        public static String addDaysToDateStr(String specificDate, int daysCount) throws ParseException {
            String format = dateFormat.format( addDaysToDate(specificDate, daysCount) );
            System.out.println("GregorianCalendar [To a Date added Number of days leads to New Date]: "+ format);
            return format;
        }
        public static Date parseDate(String aSource, String formatStr) throws ParseException {
            DateFormat dateFormat = new SimpleDateFormat(formatStr);
            dateFormat.setLenient(false);
            return dateFormat.parse(aSource);
        }
        public static String formatDate(Date aDate, String formatStr, Calendar calendar) {
            DateFormat dateFormat = new SimpleDateFormat(formatStr);
            dateFormat.setLenient(false);
            dateFormat.setCalendar(calendar);
            return dateFormat.format(aDate);
        }
        public static String formatDate(Date aDate, String formatStr) {
            DateFormat dateFormat = new SimpleDateFormat( formatStr );
            //dateFormat.setCalendar(Calendar.getInstance(TimeZone.getTimeZone("UTF")));
            return dateFormat.format(aDate);
        }
    }
    

提交回复
热议问题