SimpleDateFormat always returns all dates - android

I have a JSON that get a Date as dd-mm-yyyy, but I need convert that String to Date, but when I do the SimpleDateFormat always returns Sat May 02 00:00:00 EDT 2015 (for example) and I want 02-05-2015
this is my code:
String str = city.getString("bd_fecha");
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
date = dateFormat.parse(str); //// DATE ALWAYS RETURNS Sat May 02 00:00:00 EDT 2015(for example)

Use this, after parse you have to format also
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String dateInString = "07-06-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}

Related

Converting GMT to Local time format in android

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

Date format not working properly in Android

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

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

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

store in date format without showing all the details (GMT , time)

Is it possible to store the current date in a file but no as a string?
What I mean:
If I use :
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy");
Date d=new Date();
String formattedDate=thedate.format(d);
date.add(formattedDate);
It stores (with the format I want) in date list (which is a String List).
I want 'date' to be a Date List and store it to a file.
Is it possible to store it as dd/MM/yyyy ?Because like this:
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy");
Date d=new Date();
date.add(d);
it stores as
Thu Apr 18 17:06:14 GMT+03:00 2013
Try the above code:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
dateFormat.format(date);
This will not save the date as string(as you desire) and will format the current date according to your need. I haven't tested it right now, but as I have used this before so I am pretty sure this works. Hope this helps. If it does not then please comment.
Update
I tried following:
List<Date> d = new ArrayList<Date>();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
String s = dateFormat.format(date);
try {
d.add(dateFormat.parse(s));
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Log.d("try","NewDateString: "+ d.get(0));
Log.d("try","NewDate: "+ d.get(0).getDate()+" "+d.get(0).getMonth()+" "+(d.get(0).getYear()+1900));
It works and following was the result I got:
NewDateString: Thu Apr 18 00:00:00 EDT 2013
New Date: 18 3 2013
So what you can do is, save the date as above and when you are retrieving the date from the file you may choose to get the Day/Month/Year from the date object that you saved.

Categories

Resources