How to get next day from specified date in android? [duplicate] - android

This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
Closed 5 years ago.
I have different dates in my program like 08-Mar-2017. How can I get next or previous date from this specified date?

Calendar cal = Calendar.getInstance();
cal.setDate(your date);
cal.add(Calendar.DAY_OF_MONTH, 1);
You have to convert the string into date and set it to calendar and then you can add a day and to remove
cal.add(Calendar.DAY_OF_MONTH, -1);

Related

Android DatePicker shows wrong value of month and disable past and date not working for appointment [duplicate]

This question already has answers here:
DatePicker shows wrong value of month
(7 answers)
Closed 7 months ago.
For example, if I select December (the 12th month), then it returns 12−1=11.
final int year = calendar.get(Calendar.YEAR);
final int month = calendar.get(Calendar.MONTH);
final int day = calendar.get(Calendar.DAY_OF_MONTH);
String dDate = dayOfMonth + "/" + month + "/" + year;
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() + (7));
I will answer your question one by one:
1. Android Date Picker show the wrong value of month
From Calendar.MONTH documentation
Field number for get and set indicating the month. This is a
calendar-specific value. The first month of the year in the Gregorian
and Julian calendars is JANUARY which is 0; the last depends on the
number of months in a year.
So the first month (January) is 0 and the last month (December) is 11. That explains why when you select December, the date picker returns 11.
2. Disable past and date not working for appointment
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() + (7));
I guess you want to set max date is 7 days after today, but the code you wrote is set max date is 7 milliseconds from today, that why you see the date picker disable all dates except today.
Change your code to:
datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
calendar.add(Calendar.DAY_OF_MONTH, 7); // Add 7 days after today
datePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());

best way to get timestamp in long in android? [duplicate]

This question already has answers here:
Which one is recommended: Instant.now().toEpochMilli() or System.currentTimeMillis()
(4 answers)
JSR 310 :: System.currentTimeMillis() vs Instant.toEpochMilli() :: TimeZone
(3 answers)
Closed 3 years ago.
I want to understand this in detail on how to get this in android and which method to follow and please explain bit more to understand in better way ?
As we have some options to get this in android and find out the best.
It will be helpful if somebody explains with code how to get this.
Thanks in advance for your help
Hi I hope this will help you
//Getting the current date
Date date = new Date();
//This method returns the time in millis
long timeMilli = date.getTime();
System.out.println("Time in milliseconds using Date class: " + timeMilli);
//creating Calendar instance
Calendar calendar = Calendar.getInstance();
//Returns current time in millis
long timeMilli2 = calendar.getTimeInMillis();
System.out.println("Time in milliseconds using Calendar: " + timeMilli2);
//Java 8 - toEpochMilli() method of ZonedDateTime
System.out.println("Getting time in milliseconds in Java 8: " +
ZonedDateTime.now().toInstant().toEpochMilli());
And output fo these options will be
Time in milliseconds using Date class: 1508484583259
Time in milliseconds using Calendar: 1508484583267
Getting time in milliseconds in Java 8: 1508484583331
if we convert those long values to the date format then all three will be the same and it will be
Input 1508484583259
Input (formatted) 1,508,484,583,259
Date (Etc/UTC) Friday, October 20, 2017 7:29:43 AM UTC
Date (GMT) Friday, October 20, 2017 7:29:43 AM GMT
Date (short/short format) 10/20/17 7:29 AM
Over here I posted only one option result but all three will be the same or you can also check it by your own on online long to date convertor.
For getting timestamp in millisecond just call:
//kotlin
val tstamp = System.currentTimeMillis()
//java
long tstamp = System.currentTimeMillis();

How to allow the user to choose date only before 10 years from today from datepicker in android?

I am stuck at point where I need to ask for birthdate from user as input. I need to put restriction that user should not be able to add any date before 10 year).
I think you mean users can't add any date earlier than 10 year before right? Or your users are mostly kids aged 10 to 0?!
Since your limit date is based on current date, you have to set limit programmatically using setMinDate(long date) and setMaxDate(long date). As you can see those method works with date in millisecond so you have to get dare in millis first:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -10); //Goes 10 Year Back in time ^^
long upperLimit = calendar.getTimeInMillis(); //Get date in millisecond (epoch)
, and then set the limit using above method:
datePicker.setMaxDate(upperLimit);
You could do this:
DatePicker datePicker = (DatePicker) findViewById(R.id.event_date);
datePicker.setMinDate(dateTenYearsAgo);
More info: https://stackoverflow.com/a/18353944/4235666
try with this code in datePicker dialog:
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -10);
long tenYearBack = c.getTimeInMillis();
datePickerDialog.getDatePicker().setMinDate(tenYearBack);

How to enable dates only between current date and previous 7 days on DatePickerDialog? [duplicate]

This question already has answers here:
How set maximum date in datepicker dialog in android?
(13 answers)
Closed 6 years ago.
Following pic is a web date picker.
There, only between current date and previous 7 days are able to be selected.
I've to do this at Android using DatePickerDialog. I implemented DatePickerDialog on my EditText's OnClickListener as an example here.
The question is how to make user could not select out of that range? Any idea?
You can set minimum and maximum date.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -7);
Date result = cal.getTime();
mDatePicker.setMaxDate(System.currentTimeMillis());
mDatePicker.setMinDate(result.getTime());

How to get all days of week using week number [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am confused a lot, by this thing. Actually I have developed one view pager, which is showing data week wise. I mean first screen is for current week of the year, and its related dates. Then now I swipe the screen I want next number of weeks and its dates.
i.e. For now, if current date is 2014/01/16 then current week number is 03. But now when I swipe the screen, I want 04th week dates of January.
Thanks in advance.
void getStartEndOFWeek(int enterWeek, int enterYear){
//enterWeek is week number
//enterYear is year
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.WEEK_OF_YEAR, enterWeek);
calendar.set(Calendar.YEAR, enterYear);
SimpleDateFormat formatter = new SimpleDateFormat("ddMMM yyyy"); // PST`
Date startDate = calendar.getTime();
String startDateInStr = formatter.format(startDate);
System.out.println("...date..."+startDateInStr);
calendar.add(Calendar.DATE, 6);
Date enddate = calendar.getTime();
String endDaString = formatter.format(enddate);
System.out.println("...date..."+endDaString);
}
and also reverese
Calendar now = Calendar.getInstance();
now.set(Calendar.YEAR,2013);
now.set(Calendar.MONTH,04);//0- january ..4-May
now.set(Calendar.DATE, 04);
System.out.println("Current week of month is : " +
now.get(Calendar.WEEK_OF_MONTH));
System.out.println("Current week of year is : " +
now.get(Calendar.WEEK_OF_YEAR));

Categories

Resources