This question already has answers here:
Understanding java.util.Calendar WEEK_OF_YEAR [duplicate]
(2 answers)
Closed 7 years ago.
The following code calculates the workweek of a specific date.
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = new GregorianCalendar();
cal.setTime(df.parse("2015-12-27 08:00:00"));
System.err.printf("%d.%02d\n", cal.getWeekYear(), cal.get(Calendar.WEEK_OF_YEAR));
It currently prints 2016.01.
As I understand the work week number specification, 2016.01 is the first week having 4 days in 2016, but there is no way December 27 can belong to such week.
Is there a way to do it in Java 7 which will work for any year assuming weeks start on Monday?
Try setting Monday as first day of the week.
cal.setFirstDayOfWeek(Calendar.MONDAY);
Related
This question already has answers here:
Get date of first day of week based on LocalDate.now() in Java 8
(10 answers)
Why dec 31 2010 returns 1 as week of year?
(6 answers)
Understanding java.util.Calendar WEEK_OF_YEAR [duplicate]
(2 answers)
Closed 1 year ago.
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.WEEK_OF_YEAR, 0);
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault());
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
System.out.println(formatter1.format(cal.getTime()));
cal.add(Calendar.DAY_OF_WEEK, 6);
System.out.println(formatter1.format(cal.getTime()));
I want to get the start and end date of a given week number in Java.
One example output:
2020-12-20
2020-12-26
Another example:
2021-01-03
2021-01-09
This question already has answers here:
Java date format conversion - getting wrong month
(8 answers)
Getting wrong month when using SimpleDateFormat.parse
(3 answers)
Month issue in SimpleDateFormat class
(6 answers)
Android SimpleDateFormat format issue
(1 answer)
Closed 4 years ago.
Wrote Below Code in Android Java File to fetch Only Date which m getting but issue is not getting correct Month in my given format.
First m fetching current month as Bill Date.
Second m fetching Current month + 1 as Due Date.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
Calendar cal = Calendar.getInstance();
String billdate= dateFormat.format(cal.getTime()).toString();
cal.add(Calendar.MONTH,1);
String duedate= dateFormat.format(cal.getTime()).toString();
Output:
22-42-2018 22-42-2018
Can't identify month. Anyone please resolve the query why m not getting exact month and what changes should i make.
This question already has answers here:
Calendar returns wrong month [duplicate]
(9 answers)
Closed 5 years ago.
Something strange happens when I am trying to convert date to a milliseconds. Maybe someone can explain my this:
Calendar calendar = Calendar.getInstance();
calendar.set(2017, 9, 3, 4, 50);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss",
Locale.getDefault());
Log.i("tag", formatter.format(calendar.getTime()));
and logcat logs my out:
I/tag: 2017.10.03 04:50:34
Why months are different ?
Calendar month is zero-based (0-11) but when displayed it's in "human" version (1-12).
In the method calendar.set(), from the documentation the parameter month starts from 0.
month the value used to set the MONTH calendar field.
* Month value is 0-based. e.g., 0 for January.
This question already has answers here:
How in Java find dates for previous 2 mondays?
(3 answers)
Closed 6 years ago.
I got a date YYYY-MM-DD how can I get the Monday date of that date whatever the given date is?
Edited: got the answer from How in Java find dates for previous 2 mondays?.
LocalDate monday =
localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY);
This question already has answers here:
Why is January month 0 in Java Calendar?
(18 answers)
Closed 9 years ago.
I would like to understand why this happens, and how can i solve this small issue.
I would like to be able to get the week number from a java calendar instance after providing the day, the month and the year.
if i do:
Calendar cal=Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 11);
cal.set(Calendar.MONTH,2);
cal.set(Calendar.YEAR, 2013);
11 Feb 2013 is week 7, but if i invoke, in the above calendar instance:
int weekNumber=cal.get(Calendar.week_of_year)
I get the week number 11.
Any idea why?
I tried setting the locale but no difference, the problem is that i can only build a calendar out of these three fields, since i'm reading them from a xml file with a parser and they are in format dd-mm-yyyy with no more information that that
Months fields in Calendar are zero based. The value 2 corresponds to Calendar.MARCH. To avoid confusion, better to use the Calendar constants. You could use:
cal.set(Calendar.MONTH, Calendar.FEBRUARY);
You have used March, because Java months in Calendar are 0-based: 0 = January, 1 = February, 2 = March.
Use
cal.set(Calendar.MONTH,1);
or
cal.set(Calendar.MONTH, Calendar.FEBRUARY);
if you can use a constant. Else, subtract 1 from the month you received from your parser.