How to convert Date to a particular format in android?
This is modified code that you should use:
String date="Mar 10, 2016 6:30:00 PM";
SimpleDateFormat spf=new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aaa");
Date newDate=spf.parse(date);
spf= new SimpleDateFormat("dd MMM yyyy");
date = spf.format(newDate);
System.out.println(date);
Use hh
for hours in order to get correct time.
Java 8 and later
Java 8 introduced new classes for time manipulation, so use following code in such cases:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy h:mm:ss a");
LocalDateTime dateTime = LocalDateTime.parse(date, formatter);
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd MMM yyyy");
System.out.println(dateTime.format(formatter2));
Use h
for hour format, since in this case hour has only one digit.
How do I change date time format in Android?
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
Date format conversion Android
try this
SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
java.util.Date date = null;
try
{
date = form.parse("2011-03-27T09:39:01.607");
}
catch (ParseException e)
{
e.printStackTrace();
}
SimpleDateFormat postFormater = new SimpleDateFormat("MMMMM dd, yyyy");
String newDateStr = postFormater.format(date);
now newDateStr = March 27, 2011;
Formatting a String date into a different format in Java
Use this code to format your `2017-05-23T06:25:50'
String strDate = "2017-05-23T06:25:50";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(strDate);
SimpleDateFormat sdfnewformat = new SimpleDateFormat("MMM dd yyyy");
String finalDateString = sdfnewformat.format(convertedDate);
} catch (ParseException e) {
e.printStackTrace();
}
The converted finalDateString
set to your textView
Convert date format to different date format in android
You mixed:
mm = minutes
with
MM = months
So you have to change from:
SimpleDateFormat spf = new SimpleDateFormat("mm/dd/yy");
in to:
SimpleDateFormat spf = new SimpleDateFormat("MM/dd/yy");
When you print before changes:
System.out.println(newDate);
You will receive:
Tue Jan 07 00:04:00 WET 2020
/\
||
||
your minutes
Full working example
(with better naming)
try {
String inputDate = "04/07/20";
SimpleDateFormat inputFormat = new SimpleDateFormat("MM/dd/yy");
Date parsedDate = inputFormat.parse(inputDate);
SimpleDateFormat outputFormat = new SimpleDateFormat("dd MMMM, yy");
inputDate = outputFormat.format(parsedDate);
System.out.println(inputDate);
} catch (Exception e) {
e.printStackTrace();
}
How to convert 2021-07-06T19:27:46.811+0530 format to d MMM yyyy, hh:mm aaa this format in android
You can do it like this
Java:
SimpleDateFormat parserFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault());
SimpleDateFormat convertFormat = new SimpleDateFormat("dd/MM/yyyy, hh:mm a", Locale.getDefault());
Date date = null;
try {
date = parserFormat.parse("2021-07-06T19:27:46.811+0530");
if (date != null) {
String formatedDate = convertFormat.format(date);
Log.e("formatted date",formatedDate);
}
} catch (ParseException e) {
e.printStackTrace();
Log.e("formatted date",e.getMessage());
}
Kotlin:
val parserFormat =
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault())
val convertFormat =
SimpleDateFormat("dd/MM/yyyy, hh:mm a", Locale.getDefault())
var date: Date? = null
try {
date = parserFormat.parse("2021-07-06T19:27:46.811+0530")
if (date != null) {
val formatedDate = convertFormat.format(date)
Log.e("formatted date", formatedDate)
}
} catch (e: ParseException) {
e.printStackTrace()
Log.e("formatted date", e.message!!)
}
how to convert string to dateformat dd/MM/yyyy
Try this:
String date="15/02/2014";
SimpleDateFormat dateFormat= new SimpleDateFormat("dd/MM/yyyy");
try {
Date d=dateFormat.parse(date);
System.out.println("DATE"+d);
System.out.println("Formated"+dateFormat.format(d));
}
catch(Exception e) {
//java.text.ParseException: Unparseable date: Geting error
System.out.println("Excep"+e);
}
Hope this help.
how to convert date format in android
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);
Android - change Date Format to dd/mm/yy
You can use SimpleDateFormat
class:
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
String currentDate = sdf.format(calendar.getTime());
Converting DateTime Format to date
Use SimpleDateFormat class and you can convert it into any format you want.
String dateStr = "2014-01-09T09:33:20+0530";
SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd");
Date dateObj = curFormater.parse(dateStr);
String newDateStr = curFormater.format(dateObj);
Related Topics
How to Create a Service Which Continuously Monitors App Usage Information
Attempt to Reopen an Already-Closed Object SQLitedatabase
Best Way to Show a Loading/Progress Indicator
JSONarray Cannot Be Converted to JSONobject Error
Can an Android Device Act as an Ibeacon
Getting Net::Err_Unknown_Url_Scheme While Calling Telephone Number from HTML Page in Android
Intercept and Override Http Requests from Webview
Eclipse Hangs at The Android Sdk Content Loader
Using a Broadcast Intent/Broadcast Receiver to Send Messages from a Service to an Activity
Android/iOS - Custom Uri/Protocol Handling
Enable C++11 Support on Android
Arrayindexoutofboundsexception with Custom Android Adapter for Multiple Views in Listview
Gradle Dsl Method Not Found: 'Implementation()'
Google Maps V2 - Set Both My Location and Zoom In
Disable Android Browser's Input Overlays