Month Issue from Calendar in Android Java File [duplicate] - java

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.

Related

Why this Java code is giving different result when executed in android studio and intellij-idea? [duplicate]

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

Java - get Monday date of a week give a date in that week [duplicate]

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);

A bug in week of year in Java Calendar? [duplicate]

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);

How to get current time+n months in java? [duplicate]

This question already has answers here:
How do I add one month to current date in Java?
(18 answers)
Closed 7 years ago.
I have searched a lot on internet about how to get current time in Java but what I am looking for is how to get current time + n months?
You can try like this:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, numberofMonths);
If you can use Java 8, use LocalDate.plusMonths()
LocalDate localDate = LocalDate.now();
localDate.plusMonths(numberOfMonths);

How to get the 30 days back date in java based on given date [duplicate]

This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
How to subtract X day from a Date object in Java?
(10 answers)
Closed 9 years ago.
I have an input field, where i can select the date from date picker.Based on this selected value i need to get the 30 days back date.
Thanks in advance.
Calendar cal = Calendar.getInstance();
cale.add(Calendar.DATE, -30);
System.out.println("Date = " + cal.getTime());

Categories

Resources