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.
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:
SimpleDateFormat. format returning wrong date
(2 answers)
How do you create a coutdown on a specific date?
(1 answer)
Parse Date String Return Wrong Month
(1 answer)
Why is January month 0 in Java Calendar?
(18 answers)
Closed 2 years ago.
I am trying to take a variable of Date type . But output I am getting is automatically getting incremented by one month.
Here is what I am doing
Date d1 = new Date(2020,8,15);
System.out.println(d1);
d1's value is coming 2020-09-15 instead of 2020-08-15.
Unfortunately, month values are starting at 0, which makes an 8 be September when using the outdated API (e.g. java.util.Date).
That means to get the desired output, you would have to write (without a leading zero)
Date d1 = new Date(2020, 9, 15);
System.out.println(d1);
Fortunately, there's java.time now!
You can use it like this
public static void main(String[] args) {
LocalDate d1 = LocalDate.of(2020, 8, 15);
System.out.println(d1);
}
and it outputs
2020-08-15
Date takes months from 0 to 11 and not 1 to 12. So, new Date(2020,08,15); is actually 15th September 2020
And when date is printed/formatted, actual month is printed (values 1 to 12).
According to docs
A month is represented by an integer from 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is December.
Note : Always prefer java.time API over java.util.Date
This question already has answers here:
How to get last month/year in java?
(10 answers)
Closed 5 years ago.
Presently i want to display last one month records based on current date. So i need last month,last date & last year. In between months i need 30 days. For Ex: current date is 12/12/2017 (dd/mm/yyyy) then expected date is 13/11/2017
EX: current date is 12/03/2017 then expected date is 11/02/2017
You can simply do it using calendar.
Calendar c=Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DAY_OF_YEAR, -30);
System.out.println(c.getTime());
This should solve your problem.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
cal.add(Calendar.DATE, -1);
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);
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.