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
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.
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.
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 );
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());
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 to format date and time in Android?
Use the standard Java DateFormat class.
For example to display the current date and time do the following:
Date date = new Date(location.getTime());
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));
You can initialise a Date object with your own values, however you should be aware that the constructors have been deprecated and you should really be using a Java Calendar object.
How to change date time language in android
try this:
public static String formatTime(Date time, Locale locale){
String timeFormat = UserSettingManager
.getUserSetting(UserSettingManager.PREF_TIME_FORMAT);
if(StringUtils.isEmptyOrWhitespace(timeFormat)){
timeFormat = DEFAULT_TIME_FORMAT;
}
SimpleDateFormat formatter;
try {
formatter = new SimpleDateFormat(timeFormat, locale);
} catch(Exception e) {
formatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT, locale);
}
return formatter.format(time);
}
And then like
Log.e("CHINESE DATE", formatTime(new Date(), Locale.CHINESE));
EDIT
If you don't find a locale in the default list you can instantiate one using its constructor
:
Locale spanish = new Locale("es", "ES");
So it becomes
Log.e("SPANISH DATE", formatTime(new Date(), new Locale("es", "ES"));
Android - change Date format to - dd.mm.yyyy HH:mm
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 Topics
Libz.So.1: Cannot Open Shared Object File
How to Use Dimens.Xml in Android
How to Change the Background of an Android Tab Widget
Converting Utc Dates to Other Timezones
Change Clickable Textview's Color on Focus and Click
How to Change the Tabs Images in the Tabhost
Error Inflating Class Androidx.Constraintlayout.Constraintlayout After Migration to Androidx
How to Have a Viewpager Inside of a Scrollview
Android: Using Objectanimator to Translate a View with Fractional Values of the View's Dimension
Android Tabwidget Detect Click on Current Tab
Android Calling Asynctask Right After an Another Finished
Android: Launch Activity from Clickable Text
Android: How to Refresh Listview Contents
Android: Running a Background Task Using Alarmmanager
Changing Text Color of Menu Item in Navigation Drawer
How to Animate Marker in Android Map API V2
Can't Upload Updated APK to Google Play Due to Different Certificate