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();
}
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 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);
I am obtaining the Date and converting into the GMT format. I am obtaining it in the form of Thu Jul 24 06:55:56 GMT+05:30 2014. I want the date to be displayed in the following form 6/19/2014 12:28:44 PM. Can anyone tell me step by step how to do it. I read the following document http://developer.android.com/reference/java/text/SimpleDateFormat.html but the format tends to remain the same even if I use a and L. I am posting the code below, please guide me.
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("LL/dd/yyyy HH:mm:ss a");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("LL/dd/yyyy HH:mm:ss a");
//Time in GMT
try {
dateFormatLocal.parse( dateFormatGmt.format(new Date()) );
Log.i("gmt time",""+dateFormatLocal.parse( dateFormatGmt.format(new Date()) ));
date_edittext.setText(""+dateFormatLocal.parse( dateFormatGmt.format(new Date()) ));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
use this :
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.getDefault());
say if it works :)
SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(Date.parse("Your date string"));
String strCurrentDate = "Wed, 18 Apr 2012 07:55:29 +0000";
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
String currentDate = format.format(strCurrentDate);
format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(Date.parse(currentDate));
I don't quite get your problem but look :
Date test = new Date();
test.setHours(10);
SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy HH:mm:ss a");
System.out.println(sdf.format(test));
This displays 7/24/2014 10:13:01 AM
If I change setHours to test.setHours(22) this displays 7/24/2014 10:13:01 PM
Hope this helps.
This might be easy, and I might no see the obvious, but I'm like 1 hour trying to figure out how to convert the date from string to a format I want.
String date_begin = "15-10-2013 10:25:31";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // This is the format I want
Date startDate;
try {
startDate = df.parse(date_begin);
String startDateString1 = df.format(startDate);
Log.e("New date: ", startDateString1);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And the output is the following:
New date:(2799): 0021-04-04 10:25:31
How d'hell is displaying this output?
15-10-2013 10:25:31 is not in yyyy-MM-dd HH:mm:ss format. Parse it with dd-MM-yyyy HH:mm:ss instead.
Looks like you want to change the formatting. The solution is to use two SimpleDateFormats, one for parsing and another for formatting the output. Like this:
DateFormat inputFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
startDate = inputFormat.parse(date_begin);
String startDateString1 = outputFormat.format(startDate);
Log.e("New date: ", startDateString1);
you have to parse the date with a different format
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
The format of the date_begin , SimpleDateFormat("yyyy-MM-dd HH:mm:ss") is not same . Both should be in same format.
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")