BAEL-3912: Changes done
This commit is contained in:
parent
b60b729906
commit
2401a9af7d
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.getweeknumber;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class GetWeekNumberNewJava {
|
||||
private LocalDate date;
|
||||
|
||||
public GetWeekNumberNewJava(String day, String dayFormat) {
|
||||
date = LocalDate.parse(day, DateTimeFormatter.ofPattern(dayFormat));
|
||||
}
|
||||
|
||||
public GetWeekNumberNewJava(int year, int month, int day) {
|
||||
date = LocalDate.of(year, month, day);
|
||||
}
|
||||
|
||||
public DayOfWeek getDayOfWeek() {
|
||||
return date.getDayOfWeek();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GetWeekNumberNewJava example1 = new GetWeekNumberNewJava(2020, 3, 2);
|
||||
System.out.println(example1.getDayOfWeek());
|
||||
|
||||
GetWeekNumberNewJava example2 = new GetWeekNumberNewJava("20200302", "yyyyMMdd");
|
||||
System.out.println(example2.getDayOfWeek().getValue());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.getweeknumber;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class GetWeekNumberOldJava {
|
||||
private Calendar calendar;
|
||||
|
||||
public GetWeekNumberOldJava(String day, String dayFormat) throws ParseException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(dayFormat);
|
||||
|
||||
calendar = Calendar.getInstance();
|
||||
Date date = sdf.parse(day);
|
||||
calendar.setTime(date);
|
||||
}
|
||||
|
||||
public GetWeekNumberOldJava(int year, int month, int day) {
|
||||
calendar = Calendar.getInstance();
|
||||
calendar.set(year, month, day);
|
||||
}
|
||||
|
||||
public int getDayOfWeek() {
|
||||
return calendar.get(Calendar.DAY_OF_WEEK);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws ParseException {
|
||||
GetWeekNumberOldJava example1 = new GetWeekNumberOldJava(2020, 2, 2);
|
||||
System.out.println(example1.getDayOfWeek());
|
||||
|
||||
GetWeekNumberOldJava example2 = new GetWeekNumberOldJava("20200302", "yyyyMMdd");
|
||||
System.out.println(example2.getDayOfWeek());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue