0

I'm trying to set up a mechanism which calculates the difference between two date objects but I've got a problem. My first input which will be converted to a Date object is:

Ma 14:00

Which means 'Monday 14:00'. I'm converting this string into a date object with the following code.

try {
        date = new SimpleDateFormat("EE HH:mm", Locale.getDefault()).parse(msg);
        Log.i("intent", "Date: " + date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

If I print the Date object it is 'Mon Jan 05 14:00:00 CET 1970'. I'm only interested in the day name and the time, so I didn't bother changing the other parts of the date object.

My problem is the following: I need to know the difference between the day now (example: thursday) and the day from above (example: monday).

I tried to do the following:

    Date dateNow = new Date();
    dateNow.setHours(date.getHours());
    dateNow.setMinutes(date.getMinutes());
    dateNow.setSeconds(date.getSeconds());
    dateNow.setYear(70);
    dateNow.setMonth(0);
    Log.i("DAYS TAG", ""+dateNow);
    long diff = date.getTime() - dateNow.getTime();
    long seconds = diff / 1000;
    long minutes = seconds / 60;
    long hours = minutes / 60;
    long days = hours / 24;
    Log.i("DAYS TAG", ""+days);

This way didn't work out because I was working with the days, but the days were negative or sometimes not accurate.

Basically what I want is this:

  • Get date with upcoming day (just as above monday)
  • Get current date (thursday)
  • Calculate the difference between those days (4 days)

With these steps I found this:

        Calendar now = Calendar.getInstance();
    int weekday = now.get(Calendar.DAY_OF_WEEK);
    if(weekday != Calendar.MONDAY){
        int days = (Calendar.SATURDAY - weekday + 2) %7;
        now.add(Calendar.DAY_OF_YEAR, days);
    }

This gets the date of the upcoming monday. Just like I want in my first step. However when I added my old date, it didn't work again. It gave a wrong date back. The updated code was:

        //get difference in days between now and date object.
    Calendar old = Calendar.getInstance();
    old.setTime(date);

    Log.i("...",""+old.get(Calendar.DAY_OF_WEEK));

    Calendar now = Calendar.getInstance();
    int weekday = now.get(Calendar.DAY_OF_WEEK);
    if(weekday != Calendar.MONDAY){
        int days = (Calendar.SATURDAY - weekday + 2) %7;
        now.add(Calendar.DAY_OF_YEAR, days);
    }

    Date date2 = now.getTime();
    Log.i("tag", ""+date2);

But as I said, it gives back the wrong date.

I would really appreciate it if anyone would help. Because I've been working a few hours on this already.

Marc
  • 1,094
  • 3
  • 17
  • 38
  • You have asked multiple questions. StackOverflow works best when focused on a single sep – Basil Bourque Jul 03 '14 at 16:07
  • @BasilBourque It's based on one single problem. But I split it up in multiple parts so it becomes clear. Also, all the examples I gave are for that single problem. – Marc Jul 03 '14 at 16:11
  • Furthermore, all your questions have been asked and answered *many* times. Search before posting. You will learn to avoid the j.u.Date & .Calendar classes, to never make productive use of the toString method of j.u.Date because of its awful format and default time zone, to use Joda-Time or the new java.time libraries, to use the 3 different classes in Joda-Time that handle spans of time in different ways. – Basil Bourque Jul 03 '14 at 16:15
  • And, when asking about translating "Ma", you should mention the language. – Basil Bourque Jul 03 '14 at 16:16
  • I know about JodaTime, but I know it is possible with normal Java. I also want my .apk to become as small as possible. But if my question has been answered many times already, please provide some links because I couldn't find it with this problem (with the shortcode of the day, without a valid date known). Thanks in advance. – Marc Jul 03 '14 at 16:19
  • possible duplicate of [Calculating the Difference Between Two Java Date Instances](http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) – Basil Bourque Jul 03 '14 at 16:21
  • In that case you need to know two dates, and the problem is that I don't have those dates. I only have 2 shortcodes of days. – Marc Jul 03 '14 at 16:22
  • Joda-Time has become "normal Java" because j.u.Date and .Calendar are so bad. Even Sun/Oracle gave up on those classes, agreeing to supplant them with the new java.time package that is bundled with Java 8 (and is inspired by Joda-Time). – Basil Bourque Jul 03 '14 at 16:23
  • So search for "Joda", "current date", "plusDays", "minusDays", "elapsed", "daysBetween", "span of time", "parse", "parsing". – Basil Bourque Jul 03 '14 at 16:32
  • I prefer not to use a libary just for this small part of the program. I'll try and search for another way without extra libaries. – Marc Jul 03 '14 at 16:38

1 Answers1

0

Because this question is not a duplicate I'll paste my solution for my problem here.

                String upcomingAlarmString = Settings.System.getString(getActivity().getContentResolver(),
                    Settings.System.NEXT_ALARM_FORMATTED);
            Calendar tempDate = Calendar.getInstance();
            try{
                SimpleDateFormat format = new SimpleDateFormat("EE HH:mm", Locale.getDefault());
                tempDate.setTime(format.parse(upcomingAlarmString));
            } catch (ParseException e){
                e.printStackTrace();
            }

            Calendar alarmDate = Calendar.getInstance();
            while(alarmDate.get(Calendar.DAY_OF_WEEK) != tempDate.get(Calendar.DAY_OF_WEEK)){
                if(alarmDate.get(Calendar.DAY_OF_WEEK)==7)
                    alarmDate.set(Calendar.WEEK_OF_YEAR, alarmDate.get(Calendar.WEEK_OF_YEAR) + 1);

                alarmDate.set(Calendar.DAY_OF_WEEK, alarmDate.get(Calendar.DAY_OF_WEEK)+1);
            }

            alarmDate.set(Calendar.HOUR, tempDate.get(Calendar.HOUR));
            alarmDate.set(Calendar.MINUTE, tempDate.get(Calendar.MINUTE));
            alarmDate.set(Calendar.SECOND, 0);

            System.out.println("alarmDate: " + alarmDate.getTime());
            System.out.println("calculate difference between alarmDate and now = number of days extra");

As you can see I'm using a while loop to get the correct date.

Marc
  • 1,094
  • 3
  • 17
  • 38