change date format in datepicker dialog android - android

I have edit text field and I have a method that handles onclick event on edit field and show up a date picker dialog. However I want to change date format in the dialog . It shows Aug 31 2014, I like to show 31 08 2014. Is it possible?

This question has been answered elsewhere on the site, here for example. Basically the datepicker dialog shows up in the preferred format of the end user, so if they have their phone or emulator set to mm/dd/yyyy it will show up like that, if you want it to come up dd/mm/yyyy while you are testing it make sure your emulator is set up with that as the preferred date format.

In DatePickerDialog.OnDateSetListener you will get onDateSet method where you can get
int year = selectedYear;
int month = selectedMonth;
int day = selectedDay;
dateCalendar.set(Calendar.YEAR, year);
dateCalendar.set(Calendar.MONTH, month);
dateCalendar.set(Calendar.DAY_OF_MONTH, day);
String monthname = new SimpleDateFormat("MMM").format(dateCalendar.getTime());
in SimpleDateFormat you can change date format as you want

Related

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 set date limit in date picker

In my application i have to show dates 5 years back dates from current date like example today is 4th July 2016 but in my date picker it will show as 4th July 2011 and user can not choose date after 4th July 2011 but can choose before 4th July 2011 and backwards.
I have seen some examples like Click here
DatePickerDialog mDate = new DatePickerDialog(DatePicker.this, date, year, month, day);
maxDate = Calendar.getInstance();
maxDate.add(Calendar.YEAR, -5);
mDate.getDatePicker().setMaxDate(maxDate.getTimeInMillis());
mDate.show();
I have tried this and its working perfectly from January 1900 to July 4 2011 but one thing i want that is when i click the date in calendar it shows January 1900 all i want is when i click it it should show from July 2011 and then backward dates.
Solution-1
You can just set :
long years4 = 126144000; //4 years in seconds
long years_millis4 = years4*1000; //4 years in milliseconds, which is required format in date picker
dateDialog.getDatePicker().setMaxDate((new Date().getTime())-years_millis4);
where dateDialog is a
new DatePickerDialog()
and the param type to set for MaxDate and MinDate is a long
Hope this works for your case.
Apart from this you can add listeners to your date picker using which you can prompt user in case he chooses any date other than the required ones.
Solution 2
You can implement your custom date picker using spinners and custom dates as the list in the spinners. Apply proper conversions while getting the input.
Make sure that java.util.Date.getTime() method returns how many milliseconds have passed since January 1, 1970, 00:00:00 GMT.

Android picker with full month name

I am trying to find a way to make the DatePicker which shows the year (number) the day of the month (number) and the Month as a 3 letters abbreviation to show the very same but with the full month name. I mean that I want it to let me choose between January, February... and not Jan, Feb...
Is there a way to do that?
try this code , in your date picker
Calendar mycalender = Calendar.getInstance();
mycalender.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
try this,
SimpleDateFormat format=new SimpleDateFormat("MMMM");// also you can use: "yyyy-MMMM-dd"
String monthName=format.format(Calendar.getInstance().getTime());

How to set a date in DatePicker

I want DatePicker shows a specific date when opened, unfortunately seems I can set only the date limits with setMinDate and setMaxDate.
Is there a way to set a date without block the selection with date limits?
DatePicker date = new DatePicker(context);
date.init(year, month, day, null);
where year, month and day are ints. Here is an explanation about init

Calendar dayOfMonth off by 1

When trying to get a string for the current date using
DateFormat.getDateInstance().format(calendar.getTime())
it keeps returning the wrong day. For example, it is saying today, July 25th., is July 26th. Also when I use it to sat a date picker, I get the day value by using
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
When the date picker is set, it also shows the day ahead by 1.
To get the calendar I'm using
Calendar calendar = Calendar.getInstance();
Is there something I'm missing?
Thanks
I would imagine this is because you havent set the timezone to your timezone, and rather than the day being off randomly, the time zone you are in is diferent than GMT (Greenwich Median? Time). Try looking at this example How to handle calendar TimeZones using Java?

Categories

Resources