fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. public static void main(String[] args) throws java.lang.Exception{
  12.  
  13. // String tz = "Europe/London";
  14. String tz[] = {"IST", "Asia/Kolkata", "Europe/London"};
  15.  
  16. for(String s: tz){
  17. System.out.println("Testing timezone: "+s);
  18. System.out.println(nthWeekdayOfMonth(6, 9, 2016, 3, TimeZone.getTimeZone(s)));
  19. System.out.println(nthWeekdayOfMonth(6, 10, 2016, 3, TimeZone.getTimeZone(s)));
  20. System.out.println(nthWeekdayOfMonth(6, 11, 2016, 3, TimeZone.getTimeZone(s)));
  21. System.out.println(nthWeekdayOfMonth(6, 0, 2017, 3, TimeZone.getTimeZone(s)));
  22. System.out.println(nthWeekdayOfMonth(6, 1, 2017, 3, TimeZone.getTimeZone(s)));
  23. System.out.println(nthWeekdayOfMonth(6, 2, 2017, 3, TimeZone.getTimeZone(s)));
  24. }
  25.  
  26. }
  27.  
  28. public static Date nthWeekdayOfMonth(int dayOfWeek, int month, int year, int week, TimeZone timeZone) {
  29. Calendar calendar = Calendar.getInstance();
  30. calendar.setTimeZone(timeZone);
  31. calendar.set(GregorianCalendar.DAY_OF_WEEK, dayOfWeek);
  32. //calendar.set(Calendar.WEEK_OF_MONTH, week);
  33. calendar.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, week);
  34. calendar.set(Calendar.MONTH, month);
  35. calendar.set(Calendar.YEAR, year);
  36. return calendar.getTime();
  37. }
  38. }
Success #stdin #stdout 0.05s 711680KB
stdin
Standard input is empty
stdout
Testing timezone: IST
Fri Oct 21 05:25:06 GMT 2016
Fri Nov 18 05:25:06 GMT 2016
Fri Dec 16 05:25:06 GMT 2016
Fri Jan 20 05:25:06 GMT 2017
Fri Feb 17 05:25:06 GMT 2017
Fri Mar 17 05:25:06 GMT 2017
Testing timezone: Asia/Kolkata
Fri Oct 21 05:25:06 GMT 2016
Fri Nov 18 05:25:06 GMT 2016
Fri Dec 16 05:25:06 GMT 2016
Fri Jan 20 05:25:06 GMT 2017
Fri Feb 17 05:25:06 GMT 2017
Fri Mar 17 05:25:06 GMT 2017
Testing timezone: Europe/London
Fri Oct 21 09:55:06 GMT 2016
Fri Nov 18 10:55:06 GMT 2016
Fri Dec 16 10:55:06 GMT 2016
Fri Jan 20 10:55:06 GMT 2017
Fri Feb 17 10:55:06 GMT 2017
Fri Mar 17 10:55:06 GMT 2017