I have a small program that displays the current week from todays date, like this:
GregorianCalendar gc = new GregorianCalendar();
int day = 0;
gc.add(Calendar.DATE, day);
And then a JLabel that displays the week number:
JLabel week = new JLabel("Week " + gc.get(Calendar.WEEK_OF_YEAR));
So right now I'd like to have a JTextField where you can enter a date and the JLabel will update with the week number of that date. I'm really not sure how to do this as I'm quite new to Java. Do I need to save the input as a String? An integer? And what format would it have to be (yyyyMMdd etc)? If anyone could help me out I'd appreciate it!
WeekFields
This method that I created works for me in Java 8 and later, using
WeekFields
,DateTimeFormatter
,LocalDate
, andTemporalField
.Don't forget to format your date properly based on your use case!
You can use that, but you have to parse the date value to proper date format using
SimpleDateFormatter
ofjava API
. You can specify any format you want. After that you can do you manipulation to get the week of the year.