Get the start and end dates of the current week in Java – (Monday to Sunday)
•
Java
Today is April 6, 2014 – Sunday
The output uses the following code: –
Start date = April 7, 2014
End date = April 13, 2014
I want to output as: –
Start date = March 31, 2014
End date = April 6, 2014
// Get calendar set to current date and time Calendar c = GregorianCalendar.getInstance(); System.out.println("Current week = " + Calendar.DAY_OF_WEEK); // Set the calendar to monday of the current week c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY); System.out.println("Current week = " + Calendar.DAY_OF_WEEK); // Print dates of the current week starting on Monday DateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault()); String startDate = "",endDate = ""; startDate = df.format(c.getTime()); c.add(Calendar.DATE,6); endDate = df.format(c.getTime()); System.out.println("Start Date = " + startDate); System.out.println("End Date = " + endDate);
Solution
I believe just use:
c.setFirstDayOfWeek(Calendar.MONDAY);
Will solve your problem
The above is all the content collected and sorted out by the programming house for you. I hope this article can help you solve the program development problems encountered in obtaining the start and end dates of the current week – (Monday to Sunday) in Java.
If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码