0

I have a problem with my code in which I use strings ("2019/07/23") and I'm adding days with a separate function but when I send these new dates to another function (to give me back a value), this other function doesn't recognize these dates. but if I send these dates manually ("2019/07/24" instead of variable DATE) the other function now give me a value. i don't know why, because i compared this variables to strings ("2019/07/23".equals(date)) and they are equals...

String datex = "2019/07/23";
while(!datex.equals(dateUntil)){
        turn = diary.turns(lawyer, datex);      
        while(turn != null){
            sum++;
        }
        datex = addDay(datex, 1);
    }
Astrovik
  • 1
  • 1
  • 9
    Please help us by providing a [minimal example](https://stackoverflow.com/help/minimal-reproducible-example) reproducing the problem – Jb31 Jul 14 '19 at 15:37
  • 1
    You're getting there, but that's still not a [mre] (read the link for more information). – Slaw Jul 14 '19 at 15:48
  • I'm not sure how your prose is related to the code. Could be my fault, but could you please clarify? – Mad Physicist Jul 14 '19 at 15:49
  • haha Im sorry, it's hard for me to explain it. the thing is the first time "datex" enters the "diary.turns" function, "turn" is not null because it is declared manually (the first line). but somehow when I add a day with the function "addDay" the result ("2019/07/24") its not valid and it doesn't return anything. im sorry but i don't know how to explain it – Astrovik Jul 14 '19 at 15:55
  • 2
    @Astrovik maybe provide both version of the code, one that works and the one that do not. Also you should use java.time api for such functions – GotoFinal Jul 14 '19 at 16:01
  • 1
    thanks i will try that java.time. now im using java.text and java.util to add days. the code only works with [String datex = "2019/07/23" or other dates that i manually declare] but if i change or modify that date the functions no longer recognize it – Astrovik Jul 14 '19 at 16:08
  • This has got to be wrong: `while(turn != null){ sum++; }`. There’s nothing in the loop changing the value of `turn`, so the loop will either not execute or execute indefinitely. – Ole V.V. Jul 14 '19 at 20:33
  • Don’t use a string for keeping a date in. Use a `LocalDate` (class from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/)). Only when you need to output a string, format your `LocalDate` into one. Ths also means that the `turns` method of `diary` should accept a `LocalDate` parameter. – Ole V.V. Jul 14 '19 at 20:36

2 Answers2

2

You've not given enough detail to diagnose your problem. But I can give you simple code example of the right approach.

ISO 8601

Firstly, wherever possible, use ISO 8601 standard format for serializing date-time values as human-readable text. For a date that would be YYYY-MM-DD.

LocalDate

If using ISO 8601 formats is not possible, define a DateTimeFormatter to with a formatting pattern to match your input. Then parse as a LocalDate. The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.

String input = "2019/07/23";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/MM/dd" ) ;
LocalDate ld = LocalDate.parse( input , f ) ;

Or make your input compliant with ISO 8601.

String input = "2019/07/23".replace( "/" , "-" ) ;
LocalDate ld = LocalDate.parse( input ) ;

Get another date.

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
LocalDate today = LocalDate.now( z ) ;

Compare. Call LocalDate::isBefore, isAfter, & isEqual.

boolean overdue = ld.isBefore( today ) ;

Smart objects, not dumb strings

Stop being fixated on strings. You should be passing around objects of an appropriate type, not mere strings. Doing so will make your code more self-documenting, provide type-safety, ensure valid-values, and reduce likelihood of errors.

Change your argument type from String to LocalDate.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-1

Have you tried using the Java Date class, which comes with methods for comparing dates, including checking if a date is before or after another date?

e.g

long date2mills = 1563922800000L; //2019/07/24 in milliseconds
Date date1 = new Date(System.currentTimeMillis());
Date date2 = new Date(date2mills);
System.out.println(date1.before(date2));
System.out.println(date1.after(date2));
System.out.println(date1.compareTo(date2));

Output:

> true
> false
> -1

This produces -1 when compared, as date1 is before date2

This may also help when converting Strings into dates, so you don't have to handle millisecond values directly.

Kris Rice
  • 559
  • 1
  • 5
  • 23
  • 3
    FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 14 '19 at 18:51