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("-", ".");
Related
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);
This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 3 years ago.
I'm using the new Date() function in order to get the current date. However, on the 29th of December, 2019 (29/12/2019), the function begins to display the 29th of December, 2020 (29/12/2020), incorrectly moving the year forward 3 days early.
It then reverts back to the correct year on the 1st of January (01/01/2020), correctly displaying the correct day, month and year.
I have absolutely no idea what is causing this, or why this happens.
Has anyone experienced this before and knows why? Or knows how to fix it?
My code for it is listed below. It's a pretty simple flow of events, but as mentioned above, as soon as the date reaches the 29th of December, the year in the TextView suddenly changes to 2020 instead of 2019. This happens in future years as well (2020 to 2021 etc.)
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYYY");
String currentDate = sdf.format(new Date());
startDateTextView.setText(currentDate);
Any help will be greatly appreciated :)
Note: Use modern java.time classes for all your date-time handling code instead of using Calendar and Date.
Coming to your problem
Here in your case, you need to use dd/MM/yyyy instead of dd/MM/YYYY in date formatter. Read Y returns 2012 while y returns 2011 in SimpleDateFormat for more details.
Date c = Calendar.getInstance().getTime();
System.out.println("Current time => " + c);
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String formattedDate = df.format(c);
Please use this to get current date
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:
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 timestamp in milliseconds to string formatted time in Java
(10 answers)
Closed 9 years ago.
I am getting time in milliseconds ("1369807669") format from JSON and i have to change that in "yyyy-MM-dd'T'HH:mm:ssS" format ... How can i do it??
I used this code but i am getting different time not the actual time.
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
DateTime = df.format(millis);
But i am getting time as "1970-12-23'T'12:54:32+0000" which is wrong...
I should get time something like this "2013-05-29'T'12:06:53+0000"
you have seconds not milliseconds you should multipy your seconds * 1000
check this for your current date and check with your json milliseconds also
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ssZ");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
String currentDate = formatter.format(new Date(System
.currentTimeMillis())));
may be you are getting wrong time in json