4

I have huge Problems getting the Date without time, i want to set the time to 00:00 so i can check date differences.

My steps so far trying:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date now = new Date();
try {
    now = dateFormat.parse(dateFormat.format(now));
} catch (ParseException e1) {
    //
}

This code still takes the time, i took this similar code from this question here How do I get a Date without time in Java? answered by Shobbi

I had a better result before but the time was still set to 01:00. Anyone has a better solution for me? I need to compare Date from SQLite Database with the Actual day.

Bear in mind that CN1 Datelibs are based on JDK1.3, so im limited using methods

Community
  • 1
  • 1
kaya
  • 1,626
  • 1
  • 21
  • 27
  • Do you absolutely need the format `dd-MM-yyyy`? – Taelsin Aug 24 '16 at 14:57
  • The Format is not important in this case, i just need the possibility to check wether the date is in the past or in the future – kaya Aug 24 '16 at 14:59
  • I do it by converting Date into Calendar and then calculating difference in DAY_OF_YEAR ( consider leap year for 366 days). Gives you right difference. Calendar class should be available since jdk 1.1 – Jimmy Aug 24 '16 at 15:01
  • Next question: do you need to know how far back or forward the date is, or just if it is in the past or future? Your question and comment seem conflicting to me on this. – Taelsin Aug 24 '16 at 15:05
  • Problem here is the Calendar class of CN1, it is limited a bit. i have a Method new Calendar().getSelectedDay(), but this results in "Wed Aug 24 01:00:00 CEST 2016" – kaya Aug 24 '16 at 15:06
  • I want to set Status for objects in the past, because of the time difference here, i can not correctly set objects status for todays status, so objects im comparing from today are sorted out to be from yesterday, so the status message is not set correctly. – kaya Aug 24 '16 at 15:11
  • Can you not use Calendar class from JDK itself ? You have third party library in your classpath does not mean you have to use it unless you guys have that standard defined for your project. – Jimmy Aug 24 '16 at 15:11
  • I am limited to CN1 Libraries :( – kaya Aug 24 '16 at 15:13
  • Can you get day and year out of your dates? – Jimmy Aug 24 '16 at 15:14
  • Nope, if this was the case, it would be easy =) – kaya Aug 24 '16 at 15:15
  • Upvote and mark as answer, if my answer is what you needed. – Diamond Aug 24 '16 at 18:16

4 Answers4

3

Why not use a string? If you use the format yyyy-MM-dd a String.compareTo(otherDateString) should tell you how the two dates are related (one before, after, or the same day as the other)

For example:

String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//get the other date into a string
boolean isInThePast = (today.compareTo(otherDateString) > 0);
Taelsin
  • 1,030
  • 12
  • 24
2

You can compare dates like this without considering the Hour, Minute, Second, and Millisecond:

public static int daysBetweenNowAndDate(String date, String format) {
    try {
        DateFormat dateFormat = new SimpleDateFormat(format); //Change format to your incoming date string format
        Calendar now = Calendar.getInstance();
        now.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE), 0, 0, 0);
        now.set(Calendar.MILLISECOND, 0);

        Calendar otherDate = Calendar.getInstance();
        otherDate.setTime(dateFormat.parse(date));
        otherDate.set(otherDate.get(Calendar.YEAR), otherDate.get(Calendar.MONTH), otherDate.get(Calendar.DATE), 0, 0, 0);
        otherDate.set(Calendar.MILLISECOND, 0);

        long divisor = 86400000; // 24 hours (24 * 60 * 60 * 1000)
        long num = now.getTime().getTime() - otherDate.getTime().getTime();
        return (int) ((num + divisor - 1) / divisor);
    } catch (ParseException ex) {
        return -999999;
    }
}

Test:

System.out.println(daysBetweenNowAndDate("2016-08-22", "yyyy-MM-dd")); //2
System.out.println(daysBetweenNowAndDate("22-08-2016", "dd-MM-yyyy")); //2
System.out.println(daysBetweenNowAndDate("2016-08-22 22:45", "yyyy-MM-dd HH:mm")); //2
System.out.println(daysBetweenNowAndDate("22-08-2016 22:45:00", "dd-MM-yyyy HH:mm:ss")); //2
System.out.println(daysBetweenNowAndDate("2016-08-22 22:45", "dd-MM-yyyy HH:mm:ss")); //-999999 because the format and the date don't correspond
Diamond
  • 7,428
  • 22
  • 37
  • 1
    Thank you mate, the first FOUR lines should help me in this case(y) – kaya Aug 24 '16 at 17:55
  • 1
    You're welcome, upvote and mark as answer if it helps. – Diamond Aug 24 '16 at 17:56
  • Hey Mate, i copied your Code but i get syntax errors :\ – kaya Aug 25 '16 at 07:06
  • "The method set(int, int) in the type Calendar is not applicable for the arguments (int, int, int, int, int, int)" in Line 5. But your code directed me already to the right direction(y). i think you wanted to set each value on its own instead of a single method call in this case? – kaya Aug 25 '16 at 07:13
  • I'm curious, what version of Java are you using and on what IDE? – Diamond Aug 25 '16 at 07:18
  • Eclipse Mars.2 (4.5.2) on JDK8_102, CN1 project stuff set all to 1.8 – kaya Aug 25 '16 at 07:20
  • yeah, maybe the reason why i was not able to get the correct syntax set, because i had all the newer/better/easier syntaxes on Google and Stackoverflow – kaya Aug 25 '16 at 07:21
1

Found the answer after the several suggestions, thanks to you folks

Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR_OF_DAY, 0);
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
now.set(Calendar.MILLISECOND, 0);
now.getTime();

The important thing here is to take the import java.util.Calendar instead of the com.codename1.ui.Calendar.

Also a bit of my mistake, in this case, I used the Calendar and got confused by using wrong import :\

kaya
  • 1,626
  • 1
  • 21
  • 27
-1

Try to use Joda time library which can fix your problem

kepler
  • 11