How to get user-selected date format in Android? - android

I have been working on problem of date format selected by users in the Android Phone.
I have gone through almost many question and answers on the stackoverlow and havent got the perfect answer.
For example:-
I have selected dateformat in phone is "Sat, 31 Dec 2011"
But when I used as based question answered on stackoverflow:-
DateFormat df = android.text.format.DateFormat.getDateFormat(context.getApplicationContext());
tv.setText(df.format(date));
Above code returns me 06/08/2011 (mm/dd/yyyy) format.
But see the date format i selected on phone. How can i get the exact format?

In an application I'm currently developing I use the following, which I believe will do what you require
final String format = Settings.System.getString(getContentResolver(), Settings.System.DATE_FORMAT);
if (TextUtils.isEmpty(format)) {
dateFormat = android.text.format.DateFormat.getMediumDateFormat(getApplicationContext());
} else {
dateFormat = new SimpleDateFormat(format);
}

The DateFormat.getDateFormat works as expected. It returns one of three possible formats:
MM/DD/YYYY
DD/MM/YYYY
YYYY/MM/DD
I'm not sure how you set your "Sat, 31 Dec 2011" format, but as far as I know stock Android allows to set only one of three mentioned formats (Settings -> Date and Time -> Select Date Format).

Try this
Date inDate = inFormat.parse(in);
DateFormat outFormat = android.text.format.DateFormat.getDateFormat(context);
out=outFormat.format(inDate);

Related

How to get android system date and time format in app

How can i get current time format that is set in android. Like it is 12 hrs or 24 hrs. I can easily get date and time and put format on it but i want to get current format of time from android system.
Use the following:
DateFormat.is24HourFormat(context);
Source: https://developer.android.com/reference/android/text/format/DateFormat.html#is24HourFormat(android.content.Context)
If you want to get a String you should use android.text.format.DateFormat
DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(context);
String = dateFormat.format(new Date());

How to change hours data from 24-hour format to 12-hour format?

I'm retrieving hours data for places from a service (Factual). It comes to me in 24-hour format and I need to display it in 12-hour format. The data for a specific day comes like this:
"sunday\":[[\"12:00\",\"21:30\"]]
I can successfully retrieve the hours from the JSON. Then, using SimpleDateFormat, I can parse the string to a Date object. But, then I can't figure out how to convert them to 12;-hour format so that I can display them as "12:00 - 9:30" or "12:00pm - 9:30pm" rather than "12:00 - 21:30".
How can I go about doing this? Thanks!
EDIT:
By parsing the string of hours (i.e. "12:00") using SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm a");, I get an error from JSON saying that the value is unparseable. If I use just SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm");, then there's no error but I can't get things to show up in 12-hour format.
If you look in the simple date format syntax docs you will find that 'h' is used for 12-hour time and 'a' is used for AM/PM. You will need to extract the two times using substring before putting them through the dateformatters.
http://developer.android.com/reference/java/text/SimpleDateFormat.html
SimpleDateFormat in = new SimpleDateFormat("<input format goes here>");
Date d = in.parse(INPUT_DATE_STRING);
SimpleDateFormat out = new SimpleDateFormat("<output format goes here>");
String outDate = out.format(d);
Try this:
//Char sequence for a 12 hour format.
CharSequence DEFAULT_FORMAT_12_HOUR = "hh:mm a";
//Char sequence for a 24 hour format.
CharSequence DEFAULT_FORMAT_24_HOUR = "kk:mm";
//date is the Date object. Look for more functions in format.
DateFormat.format(DEFAULT_FORMAT_12_HOUR, date);
Let me know if it works. If you have any issue check the Date you are sending.
//This should give you the default time on the device. To show that it works.
DateFormat.format(DEFAULT_FORMAT_12_HOUR, Calendar.getInstance());

Android SimpleDateFormat for a two digit month

I have a database in my Android app that has dates formatted like this: 201274 for July 4, 2012 and 20121016 for October 16, 2012. I display the date of the DB row using SimpleDateFormat so that for today, it grabs the date 20121016 and displays 2012-10-16. The code is like so:
private void convertDBdate() {
convertDateTextView.setText(gotDt);
String dateStr = convertDateTextView.getText().toString();
SimpleDateFormat inputFormatter = new SimpleDateFormat("yyyyMd");
SimpleDateFormat outputFormatter = new SimpleDateFormat("yyyy-MM-dd");
try {
DBdate = inputFormatter.parse(dateStr);
dashDateStr = outputFormatter.format(DBdate);
convertFinalDateTV.setText(dashDateStr);
} catch (ParseException e ) {
e.printStackTrace();
}
With this code, 201274 displays fine as 2012-07-04, but two digit months display incorrectly, 20121016 shows as 2012-01-16 (January 16). The problem is in the MM and dd. I've tried yyyy-M-dd as the output date format, but that shows 2012-1-16 (again, January 16).
Do I have to somehow isolate the M value and do that month + 1 thing? If so how would that be written, and where would it go?
I don't want to have to re-write the dates in the database to 20120704 for July 4, 2012, I'd like to be able to fix it in code.
Your problem isn't that the input formatter should always be yyyyMMdd but that sometimes it should be yyyyMMdd
in the case of 201274 then an input format of MM won't apply but in the case of 20121016 then it will.
You'll need to add some logic parsing the length of the input and choosing the appropriate formatter.
Before I could suggest some logic I'd need to ask two questions
How do you represent 2012-10-01 in the database? I guess that it's 2012101 in which case...
How do you distinguish between 2012111 and 2012111? i.e. 2012-11-01 and 2012-01-11
maybe to late for this project but for future projects I would always recommend to use
Date.getTime()
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Date.html#getTime()
for storing even dates as it will take less space in the database, will make sorting faster and you just avoid problems you have right now.

Want to print date&time together in one text field in dd/mm/yyyy hh:mm format in android

I'm a newbie to android and struggling to achieve this.
I want to select date and time and also wants to add the selected date and time in one textfield with this format dd/mm/yyyy hh:mm.
I searched on this but couldn't find what I want to achieve.
any guidelines in terms of code or link would be appreciated.
Thanks
you can use SimpleDateFormat by which you can format the Date.
String format = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(format);
System.out.println(sdf.format(yourdate));
use this you can your desired format
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
like this May 17, 2012 12:03:09 PM
If you need to make Custom Date and Time Picker then you can see how in this they have make.Android Date Slider

date format in android

I am stuck in one date format
i want my date should look like this, 18th Mar 2011
and it can be 1st, 2nd,3rd that means i want to resolve for all the aspects
Plz help me out for this ASAP
Thanks in advance to all.
I think you want to change date in a 18th, 2nd, 1st, 3rd dated way, if i am not wrong then you can use simpleDateFormat class to convert date in different formats.
Before using SimpleDateFormat, just refer the SDK documentation: http://developer.android.com/reference/java/text/SimpleDateFormat.html.
To have day number with nd, th, rd (i.e. 2nd, 4th, 3rd, etc.), you can use:
F - day of week in month (Number) - 2 (2nd Wed in July)
(given in the documentation).
For example using SimpleDateFormat:
String dateStr = "03/08/2010";
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
Date dateObj = curFormater.parse(dateStr);
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy");
String newDateStr = postFormater.format(dateObj);
This is the term you are looking for: Quantity Strings (Plurals)
here is a link for it in the documentation:
Plurals
Here is a link with examples:
Examples
And One more:
Android Pluralization not working, need help
Hope this helps. so you just need to reformat the string with the date by using the examples provided and thats it.

Categories

Resources