Localizing dates in android - android

After reading the accepted answer to Date formatting based on user locale on android for german, I tested the following:
#Override
protected void onResume() {
super.onResume();
String dateOfBirth = "02/26/1974";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date date = null;
try {
date = sdf.parse(dateOfBirth);
} catch (ParseException e) {
// handle exception here !
}
// get localized date formats
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
String s = dateFormat.format(date);
dateTV.setText(s);
}
Here dateOfBirth is an english date. If I change the phone's language to German however, I see 02.26.1974. According to http://en.wikipedia.org/wiki/Date_format_by_country, the proper localized german date format is dd.mm.yyyy, so I was hoping to see "26.02.1974".
This leads to my question, is there a way to fully localize dates or is this a manual process where I must pore through my app for dates, times, etc.?

String dateOfBirth = "02/26/1974";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date date = null;
try {
date = sdf.parse(dateOfBirth);
} catch (Exception e) {
// handle exception here !
}
// get localized date formats
Log.i(this,"sdf default: "+new SimpleDateFormat().format(date)); // using my phone locale
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.US);
Log.i(this,"dateFormat US DEFAULT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.GERMAN);
Log.i(this,"dateFormat GERMAN DEFAULT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.CHINESE);
Log.i(this,"dateFormat CHINESE DEFAULT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
Log.i(this,"dateFormat US SHORT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
Log.i(this,"dateFormat GERMAN SHORT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINESE);
Log.i(this,"dateFormat CHINESE SHORT: "+dateFormat.format(date));
output is:
sdf default: 26.02.74 0:00
dateFormat US DEFAULT: Feb 26, 1974
dateFormat GERMAN DEFAULT: 26.02.1974
dateFormat CHINESE DEFAULT: 1974-2-26
dateFormat US SHORT: 2/26/74
dateFormat GERMAN SHORT: 26.02.74
dateFormat CHINESE SHORT: 74-2-26

Related

Converting GMT to Local time format in android

I am new to android developer. I convert the GMT to local mobile time. I got am /pm issues in this code. After 6'o clock evening time . I got am in conversion.
sorry for my English. Advance thanks for help.
public String formatDate(String s)
{
String outputText=null;
try {
// Tue May 21 14:32:00 GMT 2012
String inputText =s;
SimpleDateFormat inputFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm 'GMT'", Locale.US);
inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
if(android.text.format.DateFormat.is24HourFormat(CalloutAvalibality.this))
{
SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd,yy HH:mm");
// Adjust locale and zone appropriately
Date date = inputFormat.parse(inputText);
outputText= outputFormat.format(date)+" "+"Hrs";
System.out.println(outputText);
}
else
{
SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm a");
// Adjust locale and zone appropriately
Date date = inputFormat.parse(inputText);
outputText= outputFormat.format(date);
// outputText=outputText.replace("AM","am");
// outputText=outputText.replace("PM","pm");
System.out.println(outputText);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return outputText;
}
datefomater.format() will return you a string which is converted to timezone you initially set with the formatter object.
datefomater.parse() will return you a Date object which is in you local timezone
The Date object will set to default timezone
TimeZone timeZone = TimeZone.getTimeZone("America/Chicago");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.setTimeZone(timeZone);
String result = formatter.format(YOUR_DATE_OBJECT);

Android: SimpleDateFormat changes year of my date

I am using SimpleDateFormat to change format of date, shown below. But it somehow changes my year from for example 2016 to 2019 or 2018. How could I make it work correctly?
String date = "2016-10-22 13:45:46.000000";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:MM:SS");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd.mm.yyyy HH:MM");
String newFormat = formatter.format(testDate);
You must be careful to use the right casing (either upper or lower) for all the date/time pattern strings, as specified in the SimpleDateFormat Javadoc.
Uppercase M is used for "Month in year", and lowercase m is used for "Minute in hour".
Uppercase S is used for "Millisecond", and lowercase s is used for "second in minute".
This should work properly:
String date = "2016-10-22 13:45:46.000000";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");
String newFormat = formatter.format(testDate);

Android Date pattern for locale

I am building an app that will required the dates to be formatted to the locale. I have it working using the below code, however Sweden use the date format yyyy-mm-dd but it is giving me dd.mm.yyyy. I have looked into localizedpattern but struggling to find a decent example of how this is implemented or if its any different from what I am already trying to do.
public String formatDate(String dateToFormat){
Date date=null;
Configuration sysConfig = getResources().getConfiguration();
Locale curLocale = sysConfig.locale;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
date = sdf.parse(dateToFormat);
} catch (ParseException e) {
//
}
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT,curLocale);
return dateFormat.format(date);
}
The dateformat from the Netherlands is ("dd/MM/yyyy") with 4 yyyy, not 3 yyy.

How do I change date time format in Android?

I am displaying the date and time in Android with this format:
2013-06-18 12:41:24
How can I change it to the following format?
18-jun-2013 12:41 pm
Here is working code
public String parseDateToddMMyyyy(String time) {
String inputPattern = "yyyy-MM-dd HH:mm:ss";
String outputPattern = "dd-MMM-yyyy h:mm a";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date = null;
String str = null;
try {
date = inputFormat.parse(time);
str = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return str;
}
Documentation: SimpleDateFormat | Android Developers
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");
String date = format.format(Date.parse("Your date string"));
public class MainActivity extends AppCompatActivity {
private Date oneWayTripDate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String date ="2017-05-05 13:58:50 ";
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat output = new SimpleDateFormat("MMMM dd,yyyy #hh:mm:ss aa");
try {
oneWayTripDate = input.parse(date); // parse input
} catch (ParseException e) {
e.printStackTrace();
}
Log.e("===============","======currentData======"+output.format(oneWayTripDate);
}
}
Use this code like below:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");
String date = formatter.format(Date.parse("Your date string"));
Hope it will help you.
I modified Rusabh's answer for general purpose formatting of date in my project
public String formatDate(String dateToFormat, String inputFormat, String outputFormat) {
try {
Logger.e("DATE", "Input Date Date is " + dateToFormat);
String convertedDate = new SimpleDateFormat(outputFormat)
.format(new SimpleDateFormat(inputFormat)
.parse(dateToFormat));
Logger.e("DATE", "Output Date is " + convertedDate);
//Update Date
return convertedDate;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
Usage:
String inputFormat = "yyyy-MM-dd'T'HH:mmz";
String OutPutFormat = "MMMM dd', 'yyyy hh:mma";
String convertedDate = formatDate(asOfDateTime, inputFormat, OutPutFormat);
You have to set like this
Date date=new Date("Your date ");
SimpleDateFormat formatter5=new SimpleDateFormat("required format");
String formats1 = formatter5.format(date);
System.out.println(formats1);
set answer like this to set date format
Date date=new Date("Your date ");
SimpleDateFormat formatter5=new SimpleDateFormat("required format");
use this code below:
Date date=new Date("2013-06-18 12:41:24");
SimpleDateFormat formatter5=new SimpleDateFormat("dd-MM-yyyy hh:mm a");
String formats1 = formatter5.format(date);
System.out.println(formats1);
First Create a Calendar object using your Date object. Then build a String using date, year, month and etc you need. then you can use it.
You can get data using get() method in Calendar class.
We can use this format for convert the date. Pass the date as parameter
public String formatdate(String fdate)
{
String datetime=null;
DateFormat inputFormat = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat d= new SimpleDateFormat("yyyy-MM-dd");
try {
Date convertedDate = inputFormat.parse(fdate);
datetime = d.format(convertedDate);
}catch (ParseException e)
{
}
return datetime;
}
private val dateFormatter: Format = SimpleDateFormat(
android.text.format.DateFormat.getBestDateTimePattern(
Locale.getDefault(),
"dMMyyjjmmss"
),
Locale.getDefault()
)
val dateText = dateFormatter.format(Date(System.currentTimeMillis()))
This is the best choice available on Android which will format time based on device default Locate
Example output depending on Locale:
USA - 02/18/2021, 1:00:00 PM (USA uses 12 format time)
Ukraine - 18.2.2012, 13:00:00 (Ukraine uses 24 format time)
So it does all the hard job automatically for you
P.S. replace yy with yyyy if you need 2021 instead of 21, MM to MMM if you want display month as word instead of number and so on
For example "dMMMyyyyjjmmss" for USA will return:
Feb 18, 2021, 1:43:20 PM
For Russia:
18 февр. 2021 г., 13:44:18
Amazing, isn't?

how to convert date format in android

I am getting date into string in YYYY/MM/DD HH:MM:SS format.I want to change it into the mm/dd/yyyy HH:mm:ss and also it will show AM and PM how can I do this.please help me
Thank you
To get AM PM and 12 hour date format use hh:mm:ss a as string formatter WHERE hh is for 12 hour format and a is for AM PM format.
Note: HH is for 24 hour and hh is for 12 hour date format
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
Example
String date = "2011/11/12 16:05:06";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
You can use the SimpleDateFormat for the same kinds of any date operations.
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
SimpleDateFormat DesiredFormat = new SimpleDateFormat("MM/dd/yyyy HH:MM:SS a");
// 'a' for AM/PM
Date date = sourceFormat.parse("2012/12/31 03:20:20");
String formattedDate = DesiredFormat.format(date.getTime());
// Now formattedDate have current date/time
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
just use the Time class. Try something similar to this.
Time time = new Time();
time.set(Long.valueOf(yourTimeString));
If you really need a Date object just try this.
Date date = new Date(Long.parse(yourTimeString));
Use android.text.format.Time to the conversion. You can pass the time as text and it will return you the time in desired format by using timeInstance.format("");
you need to provide formatter.
Refer to following:
Time : http://developer.android.com/reference/android/text/format/Time.html
Formatter: http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html
you can use any combination of formatter string to work with it :)
you can use SimpleDateFormat or DateFormat for this here is the example
SimpleDateFormat gsdf = new SimpleDateFormat("YYYY/MM/DD HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
try{
Date d = gsdf.parse("2011/12/13 16:17:00");
System.out.println("new date format " + sdf.format(d));
}catch(Exception){
// handle exception if parse time or another by cause
}
public static String getFormatedDate(String strDate, String sourceFormate,
String destinyFormate) {
SimpleDateFormat df;
df = new SimpleDateFormat(sourceFormate);
Date date = null;
try {
date = df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
df = new SimpleDateFormat(destinyFormate);
return df.format(date);
}
and use like
getFormatedDate(strDate,
"yyyy-MM-dd HH:mm:ss", "mm/dd/yyyy")

Categories

Resources