Change String date format and set into TextView in Android?
Try like this:
String date ="29/07/13";
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yy");
SimpleDateFormat output = new SimpleDateFormat("dd MMM yyyy");
try {
oneWayTripDate = input.parse(date); // parse input
tripDate.setText(output.format(oneWayTripDate)); // format output
} catch (ParseException e) {
e.printStackTrace();
}
It's a 2-step process: you first need to parse the existing String into a Date object. Then you need to format the Date object into a new String.
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
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
Change date string format in android
SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(Date.parse("Your date string"));
UPDATE :-
As on, Date.parse("Your date string");
is deprecated.
String strCurrentDate = "Wed, 18 Apr 2012 07:55:29 +0000";
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
Date newDate = format.parse(strCurrentDate);
format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(newDate);
How Convert String to datetime in android
You can use SimpleDateFormat
for parsing String to Date.
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
try {
Date d = sdf.parse("20130526160000");
} catch (ParseException ex) {
Log.v("Exception", ex.getLocalizedMessage());
}
Now you can convert your Date
object back to String in your required format as below.
sdf.applyPattern("dd MMM yyyy hh:mm");
System.out.println(sdf.format(d));
Android change date format from String to Date and Time separated
A more robust way of accomplishing this task is provided below with a code example.
String initialStringDate = "2019-01-27T09:27:37Z";
Locale us = new Locale("US");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", us);
try {
Date date = format.parse(initialStringDate);
String stringDate = new SimpleDateFormat("yyyy/MM/dd", us).format(date);
String stringTime = new SimpleDateFormat("HH:mm", us).format(date);
String finalDateTime = stringDate.concat(" ").concat(stringTime);
Log.i("Date_and_Time", "" + finalDateTime);
} catch (ParseException e) {
e.printStackTrace();
}
Note: With the provided above code you will be able to even localize your date and time.
Kotlin Android / Java String DateTime Format, API21
Parse it to LocalDateTime
then format it:
LocalDateTime localDateTime = LocalDateTime.parse("2018-12-14T09:55:00");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
String output = formatter.format(localDateTime);
If this does not work with api21, you can use:
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");
String output = formatter.format(parser.parse("2018-12-14T09:55:00"));
or import ThreeTenABP.
How could I change the date format in android from dd-MM-yyyy HH:mm:ss to only dd-MM-yyyy HH:mm?
First thing is your date value in not in dd-MM-yyyy HH:mm:ss in format, it's should like as dd-MMM-yy HH:mm:ss.
Your date have only two digit in year and three character in month.
here is sample code for change date format.
String mStringDate = "25-Nov-15 14:23:34";
String oldFormat= "dd-MMM-yy HH:mm:ss";
String newFormat= "dd-MM-yyyy HH:mm";
String formatedDate = "";
SimpleDateFormat dateFormat = new SimpleDateFormat(oldFormat);
Date myDate = null;
try {
myDate = dateFormat.parse(mStringDate);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat(newFormat);
formatedDate = timeFormat.format(myDate);
tv_date.setText(formatedDate );
Related Topics
How to Customize the Action Mode's Color and Text
Creating a Dialogpreference from Xml
Android: Broadcastreceiver Intent to Detect Camera Photo Taken
Setsupportactionbar Toolbar Cannot Be Applied to (Android.Widget.Toolbar) Error
How to Add a Hint in Spinner in Xml
Google Geocoder Service Is Unavaliable (Coordinates to Address)
Android Can't Record Video with Front Facing Camera, Mediarecorder Start Failed: -19
Pop the Fragment Backstack Without Playing the Pop-Animation
Scoping States in Jetpack Compose
Android Studio: Default Project Directory
Edittext Settext Not Working with Fragment
Start Google Hangout from Intent in New Hangouts Android App