From API i get time as string "13:00:00Z" in this format and it is GMT 0 time zone.
I want to adjust it to my time zone which is GMT +4.
First you convert time into utc then after convert utc time to your local time zone. like example:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
Date utcDate = simpleDateFormat.parse("Your date");
SimpleDateFormat yourDateFormat = new SimpleDateFormat("dd MMM yyyy ยท HH'h'mm", Locale.getDefault());
String dateString = simpleDateFormat.format(utcDate);
binding.orderdate.setText(dateString);
} catch (Exception e) {
e.printStackTrace();
}
Related
I am new to android developer. I convert the GMT to local mobile time. I got am /pm issues in this code. After 6'o clock evening time . I got am in conversion.
sorry for my English. Advance thanks for help.
public String formatDate(String s)
{
String outputText=null;
try {
// Tue May 21 14:32:00 GMT 2012
String inputText =s;
SimpleDateFormat inputFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm 'GMT'", Locale.US);
inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
if(android.text.format.DateFormat.is24HourFormat(CalloutAvalibality.this))
{
SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd,yy HH:mm");
// Adjust locale and zone appropriately
Date date = inputFormat.parse(inputText);
outputText= outputFormat.format(date)+" "+"Hrs";
System.out.println(outputText);
}
else
{
SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm a");
// Adjust locale and zone appropriately
Date date = inputFormat.parse(inputText);
outputText= outputFormat.format(date);
// outputText=outputText.replace("AM","am");
// outputText=outputText.replace("PM","pm");
System.out.println(outputText);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return outputText;
}
datefomater.format() will return you a string which is converted to timezone you initially set with the formatter object.
datefomater.parse() will return you a Date object which is in you local timezone
The Date object will set to default timezone
TimeZone timeZone = TimeZone.getTimeZone("America/Chicago");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.setTimeZone(timeZone);
String result = formatter.format(YOUR_DATE_OBJECT);
I am trying to parse a String to a Date and it giving me right date where as time is wrong.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm aaa");
try {
Date date = formatter.parse("2015-08-20 05:00 AM");
Log.e("date", date+""); // Logcat printing Thu Aug 20 00:00:00 GMT+05:30 2015
} catch (ParseException e) {
Log.e("Error",e.toString());
}
As you can see irrespective of the time every time parsed date time showing 00:00:00
What I want is Thu Aug 20 05:00:00 GMT+05:30 2015
It seems the problem was that your pattern String specified am/pm, but was using uppercase H's for the hour characters. These indicate a 24-hour clock, which obviously doesn't use am/pm. Change the hour characters to lowercase h's, which indicate the hour in am/pm (0-11).
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
The docs for SimpleDateFormat explain the various acceptable pattern characters.
Try below code
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss z");
String dateToStr = format.format(new Date());
System.out.println("dateToStr=>" + dateToStr);
try {
Date strToDate = format.parse(dateToStr);
System.out.println("strToDate=>" + strToDate);
} catch (ParseException e) {
e.printStackTrace();
}
Maybe its related to time zone issues, at what time zone is your input?, i'd suggest to make sure your paramater is on UTC timezone and then using formatter like this :
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm aaa");
formatter .setTimeZone(TimeZone.getTimeZone("UTC"));
Try this code you missed some lines of code
SimpleDateFormat mFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aa", Locale.getDefault());
mFormatter.setTimeZone(TimeZone.getTimeZone("India"));
Date startDate = mFormatter.parse("2015-08-20 05:00 AM");
This code working fine for me.
I am working on Android application in which I am saving and getting date from ORMLITE. I am using SimpleDateFormat for the formatting of the desire date, but except this pattern yyyy-MM-dd HH:mm it is not formatting it. My current date from server with desired dates with code is given below:
try {//EEEE , MMMM dd , yyyy hh:mm a
SimpleDateFormat mDBSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if(cursor.getString(cursor.getColumnIndex("mLastMessageDate"))==null){//.equals("") ||cursor.getString(cursor.getColumnIndex("mLastMessageDate")).toString()==null){
mTxtLastMessageDate.setText("");
mTxtLastMessageLabel.setText("");
}else{
mTxtLastMessageDate.setText(mDBSDF.parse(cursor.getString(cursor.getColumnIndex("mLastMessageDate"))).toString());
}
}catch(Exception ex) {
ex.printStackTrace();
}
This is my server date:
2015-04-28 12:57:04.000297
After using above format i am getting this:
Tue Apr 28 12:57:04 GMT+04:00 2015
I want the pattern like this:
Tuesday, April 28, 2015 1:00 pm
Except above yyyy-MM-dd HH:mm format even if i am changing "-" to "," it is not working without any error
SimpleDateFormat.parse(String) returns a Date not a formatted String.
Use SimpleDateFormat.format(Date) instead.
Try this to format the date and time...
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
Try this base on your requirement.
Input date as string
protected String dateFormat(String sqlDate) {
String strDate = "";
java.util.Date utilDate;
SimpleDateFormat sqlDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");//input format
try {
Calendar calTempDate = Calendar.getInstance();
calTempDate.setFirstDayOfWeek(Calendar.MONDAY);
utilDate = sqlDateFormat.parse(sqlDate);
calTempDate.setTime(utilDate);
strDate = new SimpleDateFormat("EEEE MMMM dd,yyyy HH:mm a")
.format(calTempDate.getTime());//output format
} catch (Exception e) {
e.printStackTrace();
}
return strDate;
}
You can use this code to find out your solution.
SimpleDateFormat fromdateformate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date=fromdateformate.parse("2015-04-28 12:57:04.000297");
SimpleDateFormat todtaeFormate=new SimpleDateFormat("EEEE MMMM yyyy hh:mm aa");
String finaldate= todtaeFormate.format(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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);
For some reason this has me tearing my hair out.
I have a UNIX timestamp as a string in Android. All I want to do is format this so that it returns the date/time in the user's droid time zone.
I can convert it to a timestamp just fine, but it uses GMT rather than their localised zone.
Thanks
Use the SimpleDateFormat constructor with the Locale you need:
http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#SimpleDateFormat%28java.lang.String,%20java.util.Locale%29
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Calendar c = Calendar.getInstance();
try {
Date dt = sdf.parse("2011-03-01 17:55:15");
c.setTime(dt);
System.out.println( c.getTimeInMillis());
System.out.println(dt.toString());
} catch (ParseException e) {
System.err.println("There's an error in the Date!");
}
outputs:
1299002115000
Tue Mar 01 12:55:15 EST 2011