How can I convert String Date Format in Android? - android

I have a string that contains a date like so:
String startTime = "2014-10-11T17:00:41+0000"
I am trying to reformat that string so that it reads like so instead:
Oct 11, 2014 5:00 PM

Since Date objects do not keep time zone information, you need to specifically set the time zone offset of original date to the target formatter. Here is the complete code for transforming from one format to another while maintaining the time zone offset (+0000 in your case). More information on TimeZone class here and on how to build a proper date and time pattern string for your requirement here.
try {
DateFormat originalFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat(
"MMM dd, yyyy K:mm a", Locale.ENGLISH);
targetFormat.setTimeZone(TimeZone.getTimeZone("GMT+0000"));
Date date = originalFormat.parse("2014-10-11T17:00:41+0000");
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Output: Oct 11, 2014 5:00 PM

Use SimpleDateFormat for parse input string and represent in new format:
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Ex.:
SimpleDateFormat sdfmtIn = new SimpleDateFormat("dd/MM/yy");
SimpleDateFormat sdfmtOut= new SimpleDateFormat("dd-MMM-yyyy");
java.util.Date date = sdfmtIn.parse(strInput);
String strOutput = sdfmtOut.format(date);

Related

Parsing datetime to UTC

I have this date => 2017-10-17 10:23:30 UTC TIME
I'm parsing it like this
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
TimeZone tz = TimeZone.getDefault(); // +03:00
dateFormat.setTimeZone(tz);
Date parsed_date = dateFormat.parse("2017-10-17 10:23:30");
but this gives me this
Tue Oct 17 13:23:30 GMT+03:00 2017
I want this
2017-10-17 01:23:30
What's the problem?
Thank you guys for your help that's what i wonted i turn out that if the date in UTC you should start with parsing in the utc format that will keep the date as its the for formating it to your local timezone format it with local time zone
private String getDate(String dateString) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date value = null;
try {
value = formatter.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm aa");
dateFormatter.setTimeZone(TimeZone.getDefault());
String dt = dateFormatter.format(value);
return dt;
}
A Date does not have a time zone. So your parsing works since Tue Oct 17 13:23:30 GMT+03:00 2017 is the same point in time as 2017-10-17 10:23:30 UTC.
The time zone is only added when you try to print a date. For example:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed_date= dateFormat.parse("2017-10-17 10:23:30");
String strDate = dateFormat.format(parsed_date);
System.out.println(strDate);
prints:
2017-10-17 10:23:30
Update: to show a date in another format / time-zone you can use something like this:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed_date= dateFormat.parse("2017-10-17 10:23:30");
SimpleDateFormat printFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
printFormat.setTimeZone(TimeZone.getTimeZone("GMT+3"));
String strDate = printFormat.format(parsed_date);
System.out.println(strDate);
This prints:
2017-10-17 01:23:30

Android Use SimpleDateFormat

I get a data from server like this:
data = "2015-04-24T23:00:17+08:00"
I am in UTC+8,I want get
data = "2015-04-24 23:00:17"
I program this
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Data = formatter.parse(data);
But I get that
Fri Apr 24 23:00:17 格林尼治标准时间+0800 2015
How can I correct it?
if I use
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-M-dd HH:mm:ss",Locale.ENGLISH);
I get
Fri Apr 24 23:00:17 GMT+08:00 2015
It is not I want
You are missing a Locale.
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
See the documentation.
Once you parse the date you returned from server, you have a java.util.Date object. If you want to convert to your own format you have to again format the date object.
private String getDate(String serverDate) {
SimpleDateFormat serverFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
Date date = serverFormatter.parse(serverDate);
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = myFormatter.format(date);
return formattedDate;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
For your desired output you need to change the date format to
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);

Convert a date and time string according to UTC format

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;
}

how to convert date format in android

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")

how do i change the format of a string representing time in android?

I have a string which i retrieve from the database which is of the following format
30-08-2010 12:30:00
i need to convert that string into the following format
Aug 30 2010 12:30 PM
How do i do this?
Also tell me how to convert the string into a date object.
thank you in advance.
look around java.util.* for more info. It has nothing to do with Android.
DateFormat formatter1 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date date = (Date)formatter1.parse("30-08-2010 12:30:00");
DateFormat formatter2 = new SimpleDateFormat("MMM dd yyyy hh:mm a");
String newFormattedString = formatter2.format(date);

Categories

Resources