Last active
June 17, 2023 22:28
-
-
Save felipe-araujo/24eaf484b05f268388b539bf4a05638e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.*; | |
import java.time.format.*; | |
import java.time.temporal.ChronoField; | |
public class YearWeek { | |
public static void main(String[] args){ | |
for(int week = 1; week < 53; week++){ | |
dbYearWeekToDateRange("2023%02d".formatted(week)); | |
} | |
} | |
public static void formatYearWeek(){ | |
LocalDate date = LocalDate.of(2023, 6, 16); | |
int weekOfYear = date.get(ChronoField.ALIGNED_WEEK_OF_YEAR); | |
print(weekOfYear); | |
final String yearWeek = "202324"; | |
dbYearWeekToDateRange(yearWeek); | |
final LocalDate janFirst = LocalDate.of(2022, 1, 1); | |
print(janFirst + " has day of week of " + janFirst.get(ChronoField.ALIGNED_WEEK_OF_YEAR)); | |
} | |
public static void dbYearWeekToDateRange(final String yearWeek){ | |
final String year = yearWeek.substring(0, 4); | |
final String week = yearWeek.substring(4); | |
final int day = 1; | |
String pattern = "%s-W%s-%s".formatted(year, week, day); | |
print("Pattern to be parsed is " + pattern); | |
LocalDate parsed = LocalDate.parse(pattern, DateTimeFormatter.ISO_WEEK_DATE); | |
final int startOfWeekOffset = 1; | |
LocalDate start = parsed.plusDays(-startOfWeekOffset); | |
LocalDate end = parsed.plusDays(6 - startOfWeekOffset); | |
print("Date range for week %s:\n\t [ %s - %s ]\n".formatted(pattern, | |
df(start), | |
df(end))); | |
} | |
public static void print(Object o){ | |
System.out.println(o); | |
} | |
public static String df(LocalDate date){ | |
DateTimeFormatter f = DateTimeFormatter.ofPattern("YYYY-MMMM-dd EEEE"); | |
return f.format(date); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment