Finding the Java Date of the next Monday 6:00, for example -


i trying create code in java can determine date of next recurring time of week specified. hard explain i'll give example. march 1st (a thursday) , user wants know when next saturday 5:00 code should output march 3rd, 5:00 date variable , if march 4th, program should output march 10th , on...

the user however, can specify time of week want. done long value offsets time of week thursday 0:00 in milliseconds. i'm having trouble wrapping head around got far:

public static long findnexttimeoftheweek(long offset) {     return system.currenttimemillis() - ((system.currenttimemillis() - offset) % 604800000) + 604800000; } 

if help, appreciated. , if more clarification needed, ask. p.s. 604800000 number of milliseconds in week.

i suggest use calendar class, can manipulate , date object using calendar#gettime. see example below, want:

import java.util.calendar; import java.util.locale; import java.util.scanner;  public class main {      public static int dayofweekiwant(final string dayofweekiwantstring)             throws exception {          final int dayofweekiwant;          if ("sunday".equalsignorecase(dayofweekiwantstring)) {             dayofweekiwant = calendar.sunday;         } else if ("monday".equalsignorecase(dayofweekiwantstring)) {             dayofweekiwant = calendar.monday;         } else if ("tuesday".equalsignorecase(dayofweekiwantstring)) {             dayofweekiwant = calendar.tuesday;         } else if ("wednesday".equalsignorecase(dayofweekiwantstring)) {             dayofweekiwant = calendar.wednesday;         } else if ("thursday".equalsignorecase(dayofweekiwantstring)) {             dayofweekiwant = calendar.thursday;         } else if ("friday".equalsignorecase(dayofweekiwantstring)) {             dayofweekiwant = calendar.friday;         } else if ("saturday".equalsignorecase(dayofweekiwantstring)) {             dayofweekiwant = calendar.saturday;         } else {             throw new exception(                     "invalid input, must \"day_of_week hour\"");         }          return dayofweekiwant;     }      public static string getordinal(int cardinal) {         string ordinal = string.valueof(cardinal);          switch ((cardinal % 10)) {         case 1:             ordinal += "st";             break;         case 2:             ordinal += "nd";             break;         case 3:             ordinal += "rd";             break;         default:             ordinal += "th";         }          return ordinal;      }      public static void printdate(calendar calendar) {         string dayofweek = calendar.getdisplayname(calendar.day_of_week,                 calendar.long, locale.english);         string dayofmonth = getordinal(calendar.get(calendar.day_of_month));         int hour = calendar.get(calendar.hour_of_day);         int minute = calendar.get(calendar.minute);         system.out.printf("%s %s %02d:%02d", dayofweek, dayofmonth, hour, minute);     }      public static void main(string[] args) {          calendar calendar = calendar.getinstance();         scanner scanner = new scanner(system.in);          final int today = calendar.get(calendar.day_of_week);          system.out.println("please, input day of week want know:");          boolean inputok = false;          while (!inputok)              try {                 string input = scanner.nextline();                 string[] split = input.split("\\s");                 string dayofweekiwantstring = split[0];                  string[] splithour = split[1].split(":");                 int hour = integer.parseint(splithour[0]);                 int minute = integer.parseint(splithour[1]);                  int dayofweekiwant = dayofweekiwant(dayofweekiwantstring);                  int diff = (today >= dayofweekiwant) ? (7 - (today - dayofweekiwant))                         : (dayofweekiwant - today);                  calendar.add(calendar.day_of_week, diff);                 calendar.set(calendar.hour_of_day, hour);                 calendar.set(calendar.minute, minute);                 calendar.set(calendar.second, 0);                 calendar.set(calendar.millisecond, 0);                  inputok = true;              } catch (exception e) {                  // using generic exception explanation purpose                 // must create specific exception                 system.out                         .println("invalid input, must enter valid input in format: \"day_of_week hour\"");                 continue;             }          printdate(calendar);      } } 

more information:


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -