Add number of days to date android - android

User will input a date and he/she will input days to add to it.
Like this:
Date: 1/1/2015
Days to add: 20
The output should be 1/21/2015
I am wondering how to do that. I am beginner in android. T__T cries I tried other sources but i don't understand them at all. THANKS
THE USER WILL SUPPLY THE DATE TO ADD AND THE NUMBER OF DAYS TO ADD. All other sources only explain adding the current date with the number of days.

You need to parse the string to a date first.
SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy");
try {
Date myDate = dateParser.parse("01/01/2015");
Calendar c = Calendar.getInstance();
c.setTime(myDate);
c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + 20);
Date newDate = c.getTime();
String newFormattedDate = dateParser.format(newDate);//01/21/2015
} catch (ParseException e) {
e.printStackTrace();
//handle exception
}

Related

Android trying to parse range of dates of CalendarPickerView

I am trying to grab a range of dates from a calendar picker view and then put them into a string list to eventually be put into firebase firestore. I've been at this for some time but cannot get past the "parse" part of it. I've searched stackO and found most answers only pertain to a single date string as shown here which I have gotten to work. I'm just having issues with a list of dates.
this is what I have so far:
CalendarPickerView bookProfileCalendar;
DateFormat dateRangeInputFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.US);
SimpleDateFormat dateRangeOutputFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
List<Date> testListDate = bookProfileCalendar.getSelectedDates();
for (Date x: testListDate){
String selectedDatesFormatted = dateRangeInputFormat.format(x); //this allows the input to be read as a date in its default format
String test = String.valueOf(selectedDatesFormatted); //not sure this is necessary
// Date test2 = dateRangeInputFormat.parse(test); // <-- this gives me parse underlined red saying "unhandled exception: java.text.ParseException
Log.d(TAG, "onClick: testList dates: " + test);
}
any help or guidance is appreciated.
It turns out I just needed some sleep...and to also surround the parse part in a try/catch block. IDK why I wasn't seeing that last night. Anyhow, this is what I ended up with
List<Date> testListDate = bookProfileCalendar.getSelectedDates();
for (Date x: testListDate){ // preferred way of doing for loops now!!!
String selectedDatesFormatted = dateRangeInputFormat.format(x);
String test = String.valueOf(selectedDatesFormatted);
try {
Date test2 = dateRangeInputFormat.parse(test);
String test3 = dateRangeOutputFormat2.format(test2);
Log.d(TAG, "onClick: test3: " + test3);
} catch (ParseException e) {
e.printStackTrace();
}
``

Same Milliseconds values coming for different dates

I store my values in database by converting the date value in milliseconds,so to get the latest date on top by using order by desc query. The order is coming as required but if i enter date 02/01/2016 and 01/30/2016 both are storing same milliseconds value.
String date = "02/01/2016";
String month = date.substring(0, 2);
String day = date.substring(3, 5);
String year = date.substring(6, 10);
Calendar c1 = Calendar.getInstance();
c1.set(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
long left = c1.getTimeInMillis();
After debugging i got the following milliseconds values
02/01/2016----61414914600000
and 01/30/2016----61414914600000
Anybody knows why this happening?
Using SimpleDateFormat value I am getting different milliseconds value:
Date date;
String dtStart = "02/01/2016";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
try {
date = format.parse(dtStart);
long timeMills=date.getTime();
System.out.println("Date ->" + date);
} catch (Exception e) {
e.printStackTrace();
}
I ran your initial code and it functions almost as expected. A few points:
You mention millisecond 61414914600000. That's not correct because it's 1900 years into the future:
http://currentmillis.com/?61414914600000
I'm pretty sure you got that number from a Date object, not from a Calendar: http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#Date(int, int, int)
As Mat said the month is zero-based for Calendar and the line where you call the setter should subtract 1:
c1.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));
You answered your own question with another snippet of code but Date is deprecated, Calendar should be used instead. Your original code in the initial post was essentially correct (except the zero-based month). You should make sure that you know where your output is coming from and / or that you don't forget to build the code before running it.

android date picker set time

i would like to set the date format of my date picker like this:
yyyy-MM-dd HH:ss:mm
I hope this is the correct format for dates in databases. if not, please tell me which format is right.
also i would like to set the hour,minute and second of selected date like 0
Example:
2015-11-30 00:00:00
For this i use this code:
long dateTime = DatePicker.getCalendarView().getDate();
Date date = new Date(dateTime);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
Android Studio tells me, that setHours,setMinutes and setSeconds is deprecated. what is the new way to set this values?
Use Calendar Object instead of Date:
Calendar c = Calendar.getInstance();
c.setTimeInMillis(DatePicker.getCalendarView().getDate(););
c.set(Calendar.HOUR,00);
like wise
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
String dateString = "2015-11-30 00:00:00";
try {
Date date = df.parse(dateString);
df.applyPattern("yyyy-mm-dd hh:mm:ss");
String result = df.format(date);
}
catch (ParseException e){
e.printStackTrace();
}

need the way to put Date in Parse.com without Time and the opposite

Can't figured how to put just Date or just Time to Parse.com.
Even after SimpleDateFormat the Date objects transferring with full default missing data.
Code samples:
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
DateFormat timeFormat = new SimpleDateFormat("HH:mm");
try {
date = dateFormat.parse(mDate.getText().toString());
time = timeFormat.parse(mTime.getText().toString());
} catch (java.text.ParseException e) {
...
meeting.put(ParseConstants.KEY_DATE, date);
meeting.put(ParseConstants.KEY_TIME, time);
but on Parse.com I received 2 DATE Objects with full date parameters: date and time at both of them.
Any idea what I need to do?
Thanks a lot.
What I finally did.
I used Calender class and filled each item(year, month, day, hour, minute) while using date and time pickers. And used Calendar.getTime method to transform it to Date class that Parse.com understand.
Calendar mDateTime = Calendar.getInstance();
...
mDateTime.set(Calendar.YEAR, selectedYear);
mDateTime.set(Calendar.MONTH, selectedMonth);
mDateTime.set(Calendar.DAY_OF_MONTH, selectedDay);
...
meeting.put(ParseConstants.KEY_DATETIME, mDateTime.getTime());
So I still have to inputs for date and time and working parse date class.

standard time vs miltary time

Im trying to display the current time in a format like 7:45pm instead of 19:45 but i can't seem to find the right format option.
Time cTime= new Time(Time.getCurrentTimezone());
cTime.setToNow();
clock.setText(cTime.format("%H:%M"));
This seems to display military
You're using Time.format(), so you can refer to the C++ strftime documentation for formatting rules. Your format string should be %I:%M%P for 12-hour time.
Use SimpleDateFormat with a - meaning am/pm marker.
Example:
Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("h:mmaa");
try {
String now = dateFormat.format(cal.getTime());
} catch (ParseException e) {
e.printStackTrace();
}

Categories

Resources