2

I used a basic technique to implement a method that finds the date of the next day based on the given parameter in the format YYYY-MM-DD and returns the next day in the same format.

Can you please take a look at the code and tell me if it is inefficient or not? It works perfectly fine, but I would prefer to implement a method with more efficiency and fewer lines of code if possible. Keep in mind that any values of the month or day that are single digits numbers have to be formatted with a 0 in the tens place.

public String nextDate(String date){ //ex: 2016-01-31 -returns-> 2016-02-01
    int MMrange = 30;

    String result = ""; 
    String daystr = date.substring(8,10);
    String monthstr = date.substring(5,7);

    int day = Integer.parseInt(daystr);
    int month = Integer.parseInt(monthstr);
    int year = Integer.parseInt(date.substring(0,4));
    if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) MMrange = 31;
    else if(month==2) MMrange = 28;
    if(year%4==0&&month==2) MMrange = 29;
    if(day==MMrange){
        day =1;
        month++;
    }else if(month==12&&day==31){
        year++;
        month = 1;
        day = 1;
    }else{
        day++;
    }
    result = Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(day);
    if(month <=9&&day<=9) result = Integer.toString(year)+"-0"+Integer.toString(month)+"-0"+Integer.toString(day);
    else if(month <= 9) result = Integer.toString(year)+"-0"+Integer.toString(month)+"-"+Integer.toString(day);
    else if(day <= 9) result = Integer.toString(year)+"-"+Integer.toString(month)+"-0"+Integer.toString(day);
    return result;
}
btrballin
  • 1,420
  • 3
  • 25
  • 41
  • 3
    Have a look at `java.util.Calendar` or the `java.time` package if you are on Java 8 – Henry Feb 01 '16 at 07:34
  • I am on Java 7 actually. Haven't had the chance or know how to move to Java 8 – btrballin Feb 01 '16 at 07:36
  • @btrballin Is this homework or real work? For real work, use a date-time library. In Java 8 and later that would be [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html). If this is homework, perhaps this question should be re-opened. – Basil Bourque Feb 01 '16 at 07:41
  • @RobbyCornelissen, actually they are. Ones divisible by 100 are not unless they are also divisible by 400. There are actually 97 leap years in 400 years. – vish4071 Feb 01 '16 at 07:42
  • This is for a personal project I'm working on in my free time. – btrballin Feb 01 '16 at 07:44
  • Input `2015-12-31` and return `2015-13-01` – hk6279 Feb 01 '16 at 07:45
  • @hk6279 Good catch. I was able to fix it by modifying the `day==MMrange` if-statement – btrballin Feb 01 '16 at 07:51

3 Answers3

1

Try this...

Updated // imports...

 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
 public static String getNextdt(String  dt) {
  try {
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        final Date date = format.parse(dt);
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        System.out.println(format.format(calendar.getTime()));
        return format.format(calendar.getTime());
    } catch (Exception e) {

    }
}
prakash
  • 644
  • 4
  • 21
  • Getting an error for your 3rd line – btrballin Feb 01 '16 at 07:53
  • Pls check above code .. i have updated.. – prakash Feb 01 '16 at 07:57
  • 1
    Hey Prakash. Yes I got success. I measured the runtime of my algorithm above with yours and noticed that yours took 50,000 microseconds to execute (because of all those imported classes) while my algorithm only took 500 microseconds even though it had a lot more lines of code. So what I was looking for with this question was a better *algorithm* rather than a cleaner *implementation*. So if you could think of ways to improve my algorithm to minimize execution time, let me know. I will definitely keep your implementation in mind for future purposes, but I want faster execution for now. – btrballin Feb 01 '16 at 10:49
  • 1
    Sorry for not clarifying earlier. I mostly wanted to know if there was a better algorithmic process to generate the next day's date because I'm trying to become familiar with coding conventions to write efficient code. I felt that my algorithm had too many lines which gave me an impression that it may not be the most efficient algorithm to generate the next day's date. – btrballin Feb 01 '16 at 11:03
0

You should use a java.text.DateFormat for format and a java.util.Calendar for calculation like

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, 1);
return df.format(cal.getTime());
Julien Charon
  • 600
  • 1
  • 7
  • 21
0

A shorter way, provided that you use Java 8 is:

LocalDate localDate = LocalDate.now();
localDate.plusDays(1);
System.out.println(localDate.toString());

Hope this works for you.

Timotei
  • 78
  • 5