5. You might try my lib Time4J which offers a Jewish/Hebrew calendar and use following code: HebrewCalendar hebcal = HebrewCalendar.nowInSystemTime (); int weekOfYear = hebcal.getInt (HebrewCalendar.WEEK_OF_YEAR); It uses the default week model/definition in Israel which starts the week on Sunday (after Sabbat).Hackerrank Java Date and Time Solution. The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. You are given a date. long msYear = 1000L * 60 * 60 * 24 * 365; long msDiff = c2.getTimeInMillis () - c1.getTimeInMillis (); System.out.println ("Years diff: " + String.valueOf (msDiff / msYear)); Edit This ignores leap years. But if you are looking for an integer representation of the number of years the leap years are irrelevant for periods shorter than half a It will automatically set the date to first day of that week. // We know week number and year. int week = 3; int year = 2010; // Get calendar, clear it and set week number and year. Calendar calendar = Calendar.getInstance (); calendar.clear (); calendar.set (Calendar.WEEK_OF_YEAR, week); calendar.set (Calendar.YEAR, year); // Now get the first
You can get the day-integer like that: Calendar c = Calendar.getInstance (); c.setTime (yourdate); // yourdate is an object of type Date int dayOfWeek = c.get (Calendar.DAY_OF_WEEK); // this will for example return 3 for tuesday. If you need the output to be "Tue" rather than 3, instead of going through a calendar, just reformat the string: new
My answer assumes you agree it's best to put the year first, in year-month-date order for chronological sorting alphabetically. If not, twiddle with the format codes for your way. Using the third-party library Joda-Time 2.3 rather than the notoriously bad java.util.Date/Calendar classes. Ri651b.