In this example we want to create a calendar or date from year and day of the year. Next we will find out what day-of-week that calendar or date represent. In the code snippet below we are trying to find the 180 day of the year 2017.
Let’s see the example below.
package org.kodejava.example.util;
import java.text.DateFormatSymbols;
import java.util.Calendar;
public class DayOfYearToDayOfWeekExample {
public static void main(String[] args) {
// Create a calendar with year and day of year.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2017);
calendar.set(Calendar.DAY_OF_YEAR, 180);
// See the full information of the calendar object.
System.out.println(calendar.getTime().toString());
// Get the weekday and print it
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println("Weekday: " + weekday);
// Get weekday name
DateFormatSymbols dfs = new DateFormatSymbols();
System.out.println("Weekday: " + dfs.getWeekdays()[weekday]);
}
}
Below is the result of our program.
Thu Jun 29 13:09:59 WITA 2017
Weekday: 5
Weekday: Thursday
Latest posts by Wayan (see all)
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020