How to convert time in 12 hr format? - android

I'm getting call time through a cursor and want to display it in 12 hr format instead of 24 hr format.
Here is my code to get time from a cursor
String callDate = managedCursor.getString(dateIndex);
Date callDayTime = new Date(Long.valueOf(callDate));
and set this call day time to text view by
lastintreactionvalueTV.setText(callDayTime+"");
it's showing it like
Thu Jun 26 14:36:24 EDT 2014
What should I do to convert it into 12 hr format?

Use SimpleDateFormat to set the format that you need, for example:
Date callDayTime = new Date();
DateFormat sdf= new SimpleDateFormat("EEE, MMM dd KK:mm:ss a z yyyy",new Locale("en"));
System.out.println(sdf.format(callDayTime) );
This will output:
Thu, Jun 26 08:54:51 AM CEST 2014

Use this,
SimpleDateFormat read = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat write = new SimpleDateFormat("MMMM-dd-yy");
String op = write.format(read.parse("2014-05-17 15:45:56"));
//
SimpleDateFormat read = new SimpleDateFormat("input format");
SimpleDateFormat write = new SimpleDateFormat("output format");
String op = write.format(read.parse("your date as in input format"));

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

Convert GMT+ date to local on android

I need to convert date like Mon Nov 14 13:36:01 GMT+03:00 2016 to local. So it must be 16:36:01
Date serverDate; // Mon Nov 14 13:36:01 GMT+03:00 2016
SimpleDateFormat dateFormat = new SimpleDateFOrmat("hh:mm:ss");
String result = dateFormat.formatDate(serverDate);
Result doesn't see GMT +03:00 and gives me wrong time (13:36 instead of 16:36).
So I just needed to create formatter without ZZZZZ and set time zone to GMT.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
final Date localDate = simpleDateFormat.parse("2016-11-11T09:48:24-05:00");
SimpleDateFormat dateFormat = new SimpleDateFOrmat("hh:mm:ss");
String result = dateFormat.formatDate(localDate); //12:48:24
After this code time of localDate variable will be 12:48:24 (in case of my GMT +03:00).

How can I convert String Date Format in 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);

How to get standard format date in android

if i try the below code for eg:
i will set the date as Fri July 25 2014 10:00 AM. which gives date in milliseconds as 1402080056000,
now if i try to read the same milliseconds to date as below
long time = 1402080056000;
Date mydate = new Date(time);
mydate variable shows date as Sat Jun 25 00:10:56 IST 2014
String DateTimeString = DateFormat.getDateTimeInstance().format(new Date(time));
with the above statement in DateTimeString i get date as Jun 25 , 2014 12:10:56 AM
How to read the datetime present in 1402080056000 to Fri July 25 2014 10:00 AM
Just need to work on the format string,
String dateTimeString=
String.valueOf(new SimpleDateFormat("dd-MM-yyyy hh:mm").format(new Date(time)));
Explicitly set time zone:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String result = String.valueOf(dateFormat.format(millis));
Also, this would be useful Regarding Timezones and Java
try below code:
private String convertMilliToDate(long timestamp) {
DateFormat formatter = new SimpleDateFormat("YOUR DATE FORMAT");
// Create a calendar object that will convert the date and time value in
// milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
return formatter.format(calendar.getTime());
}
Try this:
String date= DateFormat.format("dd/MM/yyyy hh:mm:ss", new Date(date_in_milis)).toString();
Date-time work is easier using the Joda-Time library, which works on Android.
long millis = 1402080056000L;
DateTimeZone timeZoneIndia = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeIndia = new DateTime( millis, timeZoneIndia );
DateTime dateTimeFrance = dateTimeIndia.withZone( DateTimeZone.forID( "Europe/Paris" ) );
DateTime dateTimeUtc = dateTimeIndia.withZone( DateTimeZone.UTC );
To parse a string as a date-time, search StackOverflow for "Joda parse" to find many examples.

convert a date string into long in Android

I have a date string: 02/28/2013 06:20:00 PM
I have a date formater: SimpleDateFormat dateFmt = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
java.util.Date tempDate;
when I parse the string with the formater and extract the date
tempDate = dateFmt.parse(xpp.getText());
I get a date that is off by 12 hours: Thu, Feb 28, 2013 6:20 AM
What am i overlooking?
The javadocs for SimpleDateFormat say that HH is for the hour in 24-hour format (0-23). For AM/PM format (1-12) you should use hh.

Categories

Resources