Convert a String to each device Date Format [duplicate] - android

This question already has answers here:
Date formatting based on user locale on android
(8 answers)
Closed 4 years ago.
I have a string like this "2018-08-30 03:45:39". Now I want to show it on view depend on each devide's datetime formats. Ex: My phone's datetime format is dd/mm/yyyy, my friend's phone is mm/dd/yyy. Can someone help me?

You have to use toLocalizedPattern() method
Try the following code:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
String dateLocalizedFormatPattern = simpleDateFormat.toLocalizedPattern();
Log.d("DatePattern",dateLocalizedFormatPattern )

Related

How to convert String to Date in android programming [duplicate]

This question already has answers here:
How to convert "year-month-day" date format(eg: 2015-05-12-which is a value retrieved from server) to "day-month-year" format in android
(4 answers)
How can I convert "year-month-day" string of date, to day as a string in user local language
(2 answers)
Android: How can I Convert String to Date?
(7 answers)
Closed 23 days ago.
In my application I have some strings for show date such as this : 2023-1-5.
But I want convert this to 2023-01-05.
I know I can check this with if condition and when under 10 add 0 to numbers!
But I want use best way for this and automatically, not with if condition.
How can I it?
String date ="2023-1-5";
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String dateNew = simpleDateFormat.format(date);
System.out.println(dateNew);

Convert a date stored as String to Date and extract only the year in it [duplicate]

This question already has answers here:
Retrieve Month, Day and Year values from a String using Java
(8 answers)
Closed 4 years ago.
I've been trying to look for an answer for a while but couldn't find one corresponding to mine.
I have stored the Date Of Birth of a person in firebase AS a String. I've retrieve it correctly using onDataChange. But the problem arises when i need to calculate the age of that person and i can't extract the year from that String. Can anyone help me?
Thank you
Split your String and take the last value like this:
int year = Integer.parseInt(date.split("/")[2]);

Convert Epoch or Unix timestamp to date ANDROID [duplicate]

This question already has answers here:
Convert UTC Epoch to local date
(16 answers)
Closed 6 years ago.
I am creating a chat application and I want to print the date and time in chat bubble whenever the message is sent or received.
I used the below code to get and convert the date and time.
long itemLong = (long) (chatMessage.getTime()*1000);
Date itemDate = new Date(itemLong);
String itemDateStr = new SimpleDateFormat("dd-MMM HH:MM").format(itemLong);
holder.time.setText(itemDateStr);
The itemLong variable gets the value 1.4847986E15, which is converted to date and timestamp in the format I mentioned in SimpleDateFormat, but eveytime some random date and time gets displayed not at which the message was sent or received.
I tried various solutions but I am unable to get the correct date and time.
Any help is appreciated.
Edited the code to this and its working fine.
long itemLong = (long) (chatMessage.getTime()/1000);
java.util.Date d = new java.util.Date(itemLong*1000L);
String itemDateStr = new SimpleDateFormat("dd-MMM HH:mm").format(d);
holder.time.setText(itemDateStr);

Android - change Date format to - dd.mm.yyyy HH:mm [duplicate]

This question already has answers here:
How to format date and time in Android?
(26 answers)
Closed 7 years ago.
I have a Date object like this:
Fri Jan 29 13:22:57 GMT+01:00 2016
And I need it to look like this:
29.01.2016 13:22
How can I do that ?
At First Post Your Code .
Please follow How do you format date and time in Android
Code for SimpleDateFormat . Just check the logic .
SimpleDateFormat timeStampFormat = new SimpleDateFormat("dd-yyyy-MM HHmm");
Date GetDate = new Date();
String DateStr = timeStampFormat.format(GetDate);
Now replace - as .
DateStr = DateStr.replace("-", ".");

String to Date in Android [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
i have a string which means a date. it looks like this :
/Date(1448880305230+0200)/
So how to convert it to Calendar or any type of date ?
The string above in your question doesn't look like unixtime, but technically speaking you can use SimlpeDateFormat to format your date like this:
long unixSeconds = 1372339860;
// *1000 is to convert seconds to milliseconds
Date date = new Date(unixSeconds*1000L);
// the format of your date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
String formattedDate = sdf.format(date);
//Log it in
//System.out.println(formattedDate);
//Log.d(formattedDate);

Categories

Resources