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);
Related
This question already has answers here:
How to convert Date to a particular format in android?
(7 answers)
Closed 4 years ago.
How can I format a Date String to pattern dd-MMM-yyyy and return the value as a date.?
I've used SimpleDateFormat("dd-MMM-yyyy").parse(date) but the return value is like Sun Oct 07 00:00:00 GMT+06:00 2018 but I need it like 07-Oct-2018
Any easy way to format and return as a date to dd-MMM-yyyy pattern.?
You need to use DateFormat.parse(String), that will return a new Date object. For more information DateFormat
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]);
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 )
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);
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("-", ".");