Change String date format and set into TextView in Android? - android

I want to change my date format that is in as
String date ="29/07/13";
But it is showing me the error of *Unparseable date: "29/07/2013" (at offset 2)*
I want to get date in this format 29 Jul 2013.
Here is my code that i am using to change the format.
tripDate = (TextView) findViewById(R.id.tripDate);
SimpleDateFormat df = new SimpleDateFormat("MMM d, yyyy");
try {
oneWayTripDate = df.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
tripDate.setText(oneWayTripDate.toString());

Try like this:
String date ="29/07/13";
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yy");
SimpleDateFormat output = new SimpleDateFormat("dd MMM yyyy");
try {
oneWayTripDate = input.parse(date); // parse input
tripDate.setText(output.format(oneWayTripDate)); // format output
} catch (ParseException e) {
e.printStackTrace();
}
It's a 2-step process: you first need to parse the existing String into a Date object. Then you need to format the Date object into a new String.

Change the format string to MM/dd/yyyy, while parse() and use dd MMM yyyy while format().
Sample :
String str ="29/07/2013";
// parse the String "29/07/2013" to a java.util.Date object
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(str);
// format the java.util.Date object to the desired format
String formattedDate = new SimpleDateFormat("dd MMM yyyy").format(date);

DateFormat df = new SimpleDateFormat("dd / MM / yyyy, HH:mm");
String date = df.format(Calendar.getInstance().getTime());

Related

Get date from server api and convert its format?

I am currently parsing date from PHP server api and the date from api is "1970-04-22" now I need to display this date as "04-22-1970". I tried following code but its not display the exact format.
String date = jsonObject.optString("date"); // output is "1970-04-22"
SimpleDateFormat spf=new SimpleDateFormat("MMM, dd,yyyy hh:mm:ss aaa");
Date newDate=spf.parse(date);
spf= new SimpleDateFormat("MMM dd yyyy");
date = spf.format(newDate);
Log.e("date", String.valueOf(date));
String tempDate = "1970-04-22";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Date date1=null;
try {
date1 = simpleDateFormat.parse(tempDate);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat newformat = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
String dateInReqFormat = newformat.format(date1);
In these cases you have to first convert the existing format to a date and then format the date to the string as you want it .

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

How to format date in Android in dd/mm/yy 00:00AM

How to format my date in Android. I have String which gets date in below format
String format = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
Output
25 Nov 2013 17:39:00
I want this to be converted in below format
dd/mm/yy 5:39PM
I am using below code to get Date. Also how to get that format into String ?
Using SimpleDateFormat class you can do it.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh:mm");
String format = sdf.format(Calendar.getInstance().getTime());
please try follwing answer
SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
java.util.Date date = null;
try
{
date = form.parse("2011-03-27T09:39:01.607");
}
catch (ParseException e)
{
e.printStackTrace();
}
SimpleDateFormat postFormater = new SimpleDateFormat("MMMMM dd, yyyy");
String newDateStr = postFormater.format(date);
I got it, This has to go like this
Date today = new Date();
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
String date = DATE_FORMAT.format(today);
DATE_FORMAT = new SimpleDateFormat("dd/MM/yy hh:mm:ss aa");
date = DATE_FORMAT.format(today);

Convert DateString in a particular format

I have a problem that I have Date String in the format "2011-09-28T10:33:53.694+0000". I want to change this Date format in MM/DD/YYYY, don't know How? Please suggest me for right result.
Something like this. Check the details for your "from" format.
DateFormat from = new SimpleDateFormat("yyyy-MM-ddThh:mm:ss"); // not sure about the details
from.setLenient(false);
DateFormat to = new SimpleDateFormat("MM/dd/yyyy");
to.setLenient(false);
Date d = from.parse(dateStr);
System.out.println(to.format(d));
String string = "2011-09-28T10:33:53.694+0000";
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);
SimpleDateFormat outFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date date;
String output;
try {
date = inFormat.parse(string);
output = outFormat.format(date); // 28/09/2011
} catch (ParseException e) {
e.printStackTrace();
}
Do it this way ---->
Create object Date class
Date date = new Date();
Create object of Simple Date Format class
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Note -> MM - will give month in character and dd and yyyy will give date and year in numeric
Convert date into string
String s = sdf.format(date);
and you are done with it...

String to Date Conversion Problem

I am trying to convert the following string in to date, I am able to convert the string into to date object successfully,But the Problem is in this string I want to convert the time in to am/pm i.e. 12 hr format, I tried different ways but unable to get the solution.
How to get the 12hr format time from this string ?
Here is my code:
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy"); //please notice the capital M
Date date;
try {
date = formatter.parse("Fri Jul 01 10:00:00 CDT 2011");
Log.e("ThankYou Block", ""+date.toString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TRY
public static String StringToDate(String dateToParse) {
Date formatter = new Date(HttpDateParser.parse(dateToParse));
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSS");
int offset = TimeZone.getDefault().getRawOffset();
formatter.setTime(formatter.getTime() + offset);
String strCustomDateTime = dateFormat.format(formatter);
return strCustomDateTime;
}

Categories

Resources