Pergunta

I am fairly new to java development and have come accross a problem i need help with.

I need to find out the days(if applicable), hours and minutes until 8.30 am. However, as this is a "countdown to school" the code has to know that if the next 8.30 am is on saturday or sunday, it should show the time until monday 8.30AM. Again, sorry if that does not make to much sense. So the logical way i would go about approaching this is by:

  1. Working out whether tomorrow is either monday, tuesday, wednesday, thursday or friday.
  2. If it is, work out the number of hours and minutes until 8.30am tomorrow and convert that into 2 strings (hours and minutes)
  3. Else, work out the days, hours and minutes until monday 8.30 am and convert them to strings(days, hours and minutes)

    Date dt = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(dt);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    dt = c.getTime();
    DateFormat dateFormat = new SimpleDateFormat("dd");
    String datetommorow = dateFormat.format(dt);
    int date22 = Integer.parseInt(datetommorow);
    int date33 = date22 + 1;
    Integer.toString(date33);
    

As you can see from this attempt i am struggling with this. Any help would be greatly appreciated, and if you need any more infomation do not hesitate to ask

Foi útil?

Solução

        Calendar now = Calendar.getInstance();
        int currentDay = now.get(Calendar.DAY_OF_WEEK);

        Calendar school = Calendar.getInstance();
        school.add(Calendar.DAY_OF_YEAR, 1);

        if (currentDay == Calendar.SATURDAY)
        {
            school.add(Calendar.DAY_OF_YEAR, 1);
        }
        else if (currentDay == Calendar.FRIDAY)
        {
            school.add(Calendar.DAY_OF_YEAR, 2);
        }

        school.set(Calendar.HOUR_OF_DAY, 8);
        school.set(Calendar.MINUTE, 30);

        long millisLeft = school.getTimeInMillis() - now.getTimeInMillis();
        long hoursLeft = millisLeft / (60 * 60 * 1000);
        long minutesLeft =  (millisLeft % (60 * 60 * 1000)) / (60 * 1000);

Outras dicas

This should help you

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        System.out.println("Tommorow is : " + sdf.format(cal.getTime()));

        Calendar future = Calendar.getInstance(); //future time
        future.add(Calendar.DAY_OF_MONTH, 1);
        future.set(Calendar.HOUR_OF_DAY,8); // Set hours to 8'O clock
        future.set(Calendar.MINUTE,30);
        Calendar now = Calendar.getInstance(); //get current time
        long hoursDiff = (future.getTimeInMillis() - now.getTimeInMillis())/(60 * 60 * 1000);
        long minDiff = (future.getTimeInMillis() - now.getTimeInMillis())/(60 * 1000);
        System.out.println("Hours left : " + hoursDiff);
        System.out.println("Minutes left : " + minDiff);

You may want to check out Joda-Time for Android. Additionally, DateFormat is inherently unsafe in multithreaded environments and the Date API is ugly and broken in general (at least for <=1.7 afaik)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top