I am displaying the date and time in Android with this format:
2013-06-18 12:41:24
How can I change it to the following format?
18-jun-2013 12:41 pm
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
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");
String date = format.format(Date.parse("Your date string"));
public class MainActivity extends AppCompatActivity {
private Date oneWayTripDate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String date ="2017-05-05 13:58:50 ";
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat output = new SimpleDateFormat("MMMM dd,yyyy #hh:mm:ss aa");
try {
oneWayTripDate = input.parse(date); // parse input
} catch (ParseException e) {
e.printStackTrace();
}
Log.e("===============","======currentData======"+output.format(oneWayTripDate);
}
}
Use this code like below:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");
String date = formatter.format(Date.parse("Your date string"));
Hope it will help you.
I modified Rusabh's answer for general purpose formatting of date in my project
public String formatDate(String dateToFormat, String inputFormat, String outputFormat) {
try {
Logger.e("DATE", "Input Date Date is " + dateToFormat);
String convertedDate = new SimpleDateFormat(outputFormat)
.format(new SimpleDateFormat(inputFormat)
.parse(dateToFormat));
Logger.e("DATE", "Output Date is " + convertedDate);
//Update Date
return convertedDate;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
Usage:
String inputFormat = "yyyy-MM-dd'T'HH:mmz";
String OutPutFormat = "MMMM dd', 'yyyy hh:mma";
String convertedDate = formatDate(asOfDateTime, inputFormat, OutPutFormat);
You have to set like this
Date date=new Date("Your date ");
SimpleDateFormat formatter5=new SimpleDateFormat("required format");
String formats1 = formatter5.format(date);
System.out.println(formats1);
set answer like this to set date format
Date date=new Date("Your date ");
SimpleDateFormat formatter5=new SimpleDateFormat("required format");
use this code below:
Date date=new Date("2013-06-18 12:41:24");
SimpleDateFormat formatter5=new SimpleDateFormat("dd-MM-yyyy hh:mm a");
String formats1 = formatter5.format(date);
System.out.println(formats1);
First Create a Calendar object using your Date object. Then build a String using date, year, month and etc you need. then you can use it.
You can get data using get() method in Calendar class.
We can use this format for convert the date. Pass the date as parameter
public String formatdate(String fdate)
{
String datetime=null;
DateFormat inputFormat = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat d= new SimpleDateFormat("yyyy-MM-dd");
try {
Date convertedDate = inputFormat.parse(fdate);
datetime = d.format(convertedDate);
}catch (ParseException e)
{
}
return datetime;
}
private val dateFormatter: Format = SimpleDateFormat(
android.text.format.DateFormat.getBestDateTimePattern(
Locale.getDefault(),
"dMMyyjjmmss"
),
Locale.getDefault()
)
val dateText = dateFormatter.format(Date(System.currentTimeMillis()))
This is the best choice available on Android which will format time based on device default Locate
Example output depending on Locale:
USA - 02/18/2021, 1:00:00 PM (USA uses 12 format time)
Ukraine - 18.2.2012, 13:00:00 (Ukraine uses 24 format time)
So it does all the hard job automatically for you
P.S. replace yy with yyyy if you need 2021 instead of 21, MM to MMM if you want display month as word instead of number and so on
For example "dMMMyyyyjjmmss" for USA will return:
Feb 18, 2021, 1:43:20 PM
For Russia:
18 февр. 2021 г., 13:44:18
Amazing, isn't?
Related
How to format my date in Android. I have String which gets date in below format
String format = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
Output
25 Nov 2013 17:39:00
I want this to be converted in below format
dd/mm/yy 5:39PM
I am using below code to get Date. Also how to get that format into String ?
Using SimpleDateFormat class you can do it.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh:mm");
String format = sdf.format(Calendar.getInstance().getTime());
please try follwing answer
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);
I got it, This has to go like this
Date today = new Date();
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
String date = DATE_FORMAT.format(today);
DATE_FORMAT = new SimpleDateFormat("dd/MM/yy hh:mm:ss aa");
date = DATE_FORMAT.format(today);
I am trying to format the date string to Date, and then get the month/day from it:
String strDate="2013-05-15T10:00:00-07:00";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss-z");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat sdfmonth = new SimpleDateFormat("MM/dd");
String monthday= sdfmonth.format(convertedDate);
but it returns me current month/day i.e 5/18. Whats wrong?
3 things :
There is a mistake in your format : 2013-05-15T10:00:00-07:00 has no meaning, it should be 2013-05-15T10:00:00-0700 (with no colon at the end, this is a timezone defined in RFC 822. (Look at the docs about Z).
Change your format to yyyy-MM-dd'T'HH:mm:ssZ as mentionned by #blackbelt
you get a bad date because you re-format your date whatever happens during parsing. Re-format in your try block, if and only if parsing worked.
----------Update
String strDate = "2013-05-15T10:00:00-0700";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(strDate);
SimpleDateFormat sdfmonth = new SimpleDateFormat("MM/dd");
String monthday = sdfmonth.format(convertedDate);
} catch (ParseException e) {
e.printStackTrace();
}
I don't know what is wrong in your code. for me it throws Unparseable exception like this.
java.text.ParseException: Unparseable date: "2013-05-15T10:00:00-07:00"
But the following way works well.
String strDate="January 2, 2010";
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy");
Date date = dateFormat.parse(strDate);
System.out.println(date.getMonth());
But in java Date is deprecated as per http://docs.oracle.com/ . Try to use Calender instead of Date.
I hope this will help you.
I am having a date string 2012-11-21 13:11:25 which I get from local database. I have to convert this according to UTC settings and display it on a particular screen. So if its GMT+05:30 it should be displayed as 2012-11-21 18:41:25 on the screen. How can I do this conversion. I have checked some of the questions but that didn't work out.
I am able to get a Date object that returns something like Wed Nov 21 13:11:25 GMT+05:30 2012 after this I need to get the time as 18:41:25 and date as 11-21-2012
Thanks in advance
Your df and inputFmt must use the same format.
But I think you should do it like this:
Date myDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
calendar.setTime(myDate);
Date time = calendar.getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("MMM dd, yyy h:mm a zz");
String dateAsString = outputFmt.format(time);
System.out.println(dateAsString);
Get UTC from current time :
public String getCurrentUTC(){
Date time = Calendar.getInstance().getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outputFmt.setTimeZone(TimeZone.getTimeZone("UTC"));
return outputFmt.format(time);
}
Best way to get formatted string of Date in required format is
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String formatted = dateFormat.format(date);
//This is my input date
String dtStart = "2019-04-24 01:22 PM";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
Date date = null;
try {
date = format.parse(dtStart);
getDateInUTC(date)
} catch (ParseException e) {
e.printStackTrace();
}
//This Method use for convert Date into some UTC format
public static String getDateInUTC(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateAsString = sdf.format(date);
System.out.println("UTC" + dateAsString);
return dateAsString;
}
I am getting date into string in YYYY/MM/DD HH:MM:SS format.I want to change it into the mm/dd/yyyy HH:mm:ss and also it will show AM and PM how can I do this.please help me
Thank you
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);
You can use the SimpleDateFormat for the same kinds of any date operations.
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
SimpleDateFormat DesiredFormat = new SimpleDateFormat("MM/dd/yyyy HH:MM:SS a");
// 'a' for AM/PM
Date date = sourceFormat.parse("2012/12/31 03:20:20");
String formattedDate = DesiredFormat.format(date.getTime());
// Now formattedDate have current date/time
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
just use the Time class. Try something similar to this.
Time time = new Time();
time.set(Long.valueOf(yourTimeString));
If you really need a Date object just try this.
Date date = new Date(Long.parse(yourTimeString));
Use android.text.format.Time to the conversion. You can pass the time as text and it will return you the time in desired format by using timeInstance.format("");
you need to provide formatter.
Refer to following:
Time : http://developer.android.com/reference/android/text/format/Time.html
Formatter: http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html
you can use any combination of formatter string to work with it :)
you can use SimpleDateFormat or DateFormat for this here is the example
SimpleDateFormat gsdf = new SimpleDateFormat("YYYY/MM/DD HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
try{
Date d = gsdf.parse("2011/12/13 16:17:00");
System.out.println("new date format " + sdf.format(d));
}catch(Exception){
// handle exception if parse time or another by cause
}
public static String getFormatedDate(String strDate, String sourceFormate,
String destinyFormate) {
SimpleDateFormat df;
df = new SimpleDateFormat(sourceFormate);
Date date = null;
try {
date = df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
df = new SimpleDateFormat(destinyFormate);
return df.format(date);
}
and use like
getFormatedDate(strDate,
"yyyy-MM-dd HH:mm:ss", "mm/dd/yyyy")
I have a problem that I have Date String in the format "2011-09-28T10:33:53.694+0000". I want to change this Date format in MM/DD/YYYY, don't know How? Please suggest me for right result.
Something like this. Check the details for your "from" format.
DateFormat from = new SimpleDateFormat("yyyy-MM-ddThh:mm:ss"); // not sure about the details
from.setLenient(false);
DateFormat to = new SimpleDateFormat("MM/dd/yyyy");
to.setLenient(false);
Date d = from.parse(dateStr);
System.out.println(to.format(d));
String string = "2011-09-28T10:33:53.694+0000";
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);
SimpleDateFormat outFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date date;
String output;
try {
date = inFormat.parse(string);
output = outFormat.format(date); // 28/09/2011
} catch (ParseException e) {
e.printStackTrace();
}
Do it this way ---->
Create object Date class
Date date = new Date();
Create object of Simple Date Format class
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Note -> MM - will give month in character and dd and yyyy will give date and year in numeric
Convert date into string
String s = sdf.format(date);
and you are done with it...