Is there another way to add days in android calendar picker [duplicate] - java

This question already has answers here:
SimpleDateFormat ignoring month when parsing
(4 answers)
How to add one day to a date? [duplicate]
(18 answers)
When Using Date Picker In Android It is Picking the Wrong Date [duplicate]
(1 answer)
Closed 3 years ago.
I'm trying to add days that exceed the month days. example July 1,2019 and I add 32 days so the result would be August 2,2019.
SimpleDateFormat format = new SimpleDateFormat("mm/dd/yyy");
SimpleDateFormat Dateformat = new SimpleDateFormat("mm/dd/yyy");
String getDate = date_pick.getText().toString();
Date mDate;
Date result_desu;
try {
mDate = format.parse(getDate);
Calendar calendar = Calendar.getInstance();
calendar.setTime(mDate);
calendar.add(Calendar.DATE, 32);
String formattedDate = Dateformat.format(calendar.getTime());
date_result.setText(formattedDate); // format output
} catch (ParseException e) {
e.printStackTrace();
}
I've been using this code but it turns out the days only reset with the same month example: July 1,2019 ; result: July 2,2019.

Try this:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.text.ParseException;
class Main{
public static void main(String args[]){
String oldDate = "2019-07-1";
System.out.println("Date before Addition: "+oldDate);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try{
c.setTime(sdf.parse(oldDate));
}catch(ParseException e){
e.printStackTrace();
}
c.add(Calendar.DAY_OF_MONTH, 32);
String newDate = sdf.format(c.getTime());
System.out.println("Date after Addition: "+newDate);
}
}
Output:
Date before Addition: 2019-07-1
Date after Addition: 2019-08-02

Related

how to format string datetime in android [duplicate]

This question already has answers here:
Change date string format in android
(8 answers)
Date format conversion Android
(9 answers)
Closed 2 years ago.
I get datetime data from soap web service to get string:2020-05-03T00:00:00.
Is there a way to split the string into dd / mm / yyyy, and if it is null then omitted?
I am using a substring(0,10) , but it doesn't work very well.
You can do it this way.
Parse the input string.
LocalDateTime dateTime = LocalDateTime.parse("2020-05-03T00:00:00");
Generate text representing the value of that LocalDateTime object.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String dateTimeString = dateTime.format(formatter);
System.out.println(dateTimeString);
03/05/2020
For early Android before 26, see the ThreeTenABP & ThreeTen-Backport projects, a back-port of most of the java.time functionality.
try with this
try {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = null;
date = inputFormat.parse("2020-05-03T00:00:00");
String formattedDate = outputFormat.format(date);
System.out.println("coverted: "+formattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Output: 03-05-2020
You can use java Calendar class to get Date, Month and Year as shown in below
String s = "2020-05-03T00:00:00";
Calendar calendar = new GregorianCalendar();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date dateObj = null;
try {
dateObj = format.parse(s);
calendar.setTime(dateObj);
int date = calendar.get(Calendar.DATE);
int month = calendar.get(Calendar.MONTH) + 1;// As start from 0
int year = calendar.get(Calendar.YEAR);
System.out.println("Formatted Date -> "+ date + "/" + month + "/" +year);
} catch (ParseException e) {
e.printStackTrace();
}
You can do many more thing after converting it to Calendar class object, like hour, minutes, day of week etc

Java WEEK_OF_YEAR [duplicate]

This question already has answers here:
Why dec 31 2010 returns 1 as week of year?
(6 answers)
Closed 5 years ago.
I implemented the following method:
private int getWeek(String datum){
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
Date date = null;
try {
date = format.parse(datum);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone( "Europe/Berlin" ));
calendar.setTime(date);
int week = calendar.get(Calendar.WEEK_OF_YEAR);
return week;
}
But when I call the method with
getWeek("01.01.2017")
it returns 52. But it should be 1.
Where is my mistake?
When i call the method with
getWeek("01.01.2016")
it returns 53.
you have lost set the TimeZone in SimpleDateFormat, for example:
private int getWeek(String datum) {
TimeZone zone = TimeZone.getTimeZone("Europe/Berlin");
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
// v--- set the timezone here
format.setTimeZone(zone);
Date date = null;
try {
date = format.parse(datum);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance(zone);
calendar.setTime(date);
int week = calendar.get(Calendar.WEEK_OF_YEAR);
return week;
}
Damnit.. that was a huge brain failure.
52 is the correct answer for the input.
in Germany the first Week of Year is the first week with 4 or more days in the new year.
Sorry.

Java - Calendar.getTime does not retrieve the date set with Calendar.setTime

I have the following code in Java
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Test {
public static void main(String[] args) {
try {
SimpleDateFormat SDF = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss");
Calendar date = Calendar.getInstance();
date.setTime(SDF.parse("2011-02-01T00:00:00"));
System.out.println(SDF.format(date.getTime()));
} catch (Exception e) {
}
}
}
I expect to see in the console the following string
2011-02-01T00:00:00
instead I see
2011-12-26T00:00:00
What can be wrong?
I change the format: "yyyy-MM-dd'T'HH:mm:ss"
SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Calendar date = Calendar.getInstance();
date.setTime(SDF.parse("2011-02-01T00:00:00"));
System.out.println(SDF.format(date.getTime()));
The output is 2011-02-01T00:00:00
"Y": week year
"y": year
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
From the documentation:
"If week year 'Y' is specified and the calendar doesn't support any week years, the calendar year ('y') is used instead. The support of week years can be tested with a call to getCalendar().isWeekDateSupported()."
In some calendar "Y" and "y" are the same, but is not the case of the gregorian calendar.
https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#isWeekDateSupported%28%29

how can i get a month and year between two dates [duplicate]

This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
Closed 7 years ago.
I need to get difference between two dates using Java. I need my result to be in months and year of month.
Example:
Startdate = 2015-04-03 enddate = 2015-05-03 Result should be APR-MAY 2015
Startdate = 2015-12-03 enddate = 2015-01-03 Result should be DEC-2015,JAn-2016
i need to set that value into textview how can i set this plz help me .
String startdate = "2015-11-30";
String enddate = "2016-1-30";
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormater = new SimpleDateFormat("MMM-yyyy");
Calendar beginCalendar = Calendar.getInstance();
Calendar finishCalendar = Calendar.getInstance();
try {
beginCalendar.setTime(formater.parse(startdate));
finishCalendar.setTime(formater.parse(enddate));
if (beginCalendar.get(Calendar.MONTH) != finishCalendar.get(Calendar.MONTH)){
beginCalendar.set(Calendar.DAY_OF_MONTH, 1);
finishCalendar.set(Calendar.DAY_OF_MONTH, 2);
}
do {
// add one month to date per loop
String month_year = outputFormater.format(beginCalendar.getTime());
Log.d("Date_Range", month_year);
beginCalendar.add(Calendar.MONTH, 1);
} while (beginCalendar.before(finishCalendar));
} catch (ParseException e) {
e.printStackTrace();
}
So by this you will get month and year between start date and end date in MMM-yyyy format. You can handle the result in the way you want by splitting month_year string # "-" separator.
You can use SimpleDateFormat.
EDIT: Try this
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
Check if your dates are in the same year by getting the year of the calendar.
int year1=Integer.pareInt(formatter.format(calendar1.getTime()));
int year2=Integer.pareInt(formatter.format(calendar2.getTime()));
year=year1-year2;
and then print result based on the year
formatter=new SimpleDateFormat("MMM");
if(year==0)
System.out.println(formatter.format(calendar1)+"-"+formatter.format(calendar2)+" "+ year);
else
System.out.println(formatter.format(calendar1)+"-"+year1+","+formatter.format(calendar2)+"-"+year2);

Java code for date functions [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 8 years ago.
Can any one help me with the code for adding some number of days to any date..?
For example today is 11-04-2014. I want 15-04-2014 + 3 days output:18-04-2014.
My question is not adding dates to current date..
With Java 8, you can write:
import java.time.LocalDate;
LocalDate date = LocalDate.of(2014, 4, 11);
LocalDate newDate = date.plusDays(3);
System.out.println(newDate); // Prints 2014-04-14
Its that simple.
String dateString = "11-04-2014" // Say you have a date in String format
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy"); // Create an instance of SimpleDateFormat with the right format.
Date date = format.parse(dateString); // Then parse the string, this will need a try catch statement.
Calendar calendar = Calendar.getInstance(); // Get an instance of the calendar.
calendar.setTime(date); // Set the time of the calendar to the parsed date
calendar.add(Calendar.DATE, 3); // Add the days to the calendar
String outputFormat = format.format(calendar.getTime());
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class A {
public static void main(String[] args) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, 1);
calendar.set(Calendar.YEAR, 2012);
calendar.add(Calendar.DAY_OF_MONTH, 3);
System.out.println(simpleDateFormat.format(calendar.getTime()));
}
}
You can use the calendar function:
Calendar cal = Calendar.getInstance();
cal.setTime(dateInstance);
cal.add(Calendar.DATE, NO_OF_DAYS_TO_ADD);
Date addedDays = cal.getTime();
DateInstance is the date you are using. addedDays can be formatted using SimpleDateFormat to display in any date format that you would like to use.

Categories

Resources