Hello I have been trying to format this date but it keeps giving me in unparsable date error? I am trying to get a time stamp like 2011-06-24T19:55:37Z to be June 24, 2011. here is the code I am using. Also on a side note is contraction (like the 1st, 2nd, 3rd) possible?
SimpleDateFormat sdf = new SimpleDateFormat("MM d, yyyy", Locale.US);
Date dt = sdf.parse("2011-03-01T17:55:15Z");
time.setText("Time: " + dt.toString());
The problem is that the format provided to SimpleDateFormat's constructor doesn't match the format of your date.
The string MM d, yyyy tells SimpleDateFormat how to interpret 2011-03-01T17:55:15Z.
Building a format string is described in the docs.
This comes from another question within stackoverflow
Date date = new Date(location.getTime());
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));
DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd HH:MM:SS -05:00");
Date date = (Date)formatter.parse("2011-06-24T19:55:37Z");
Make sure the SimpleDateFormat matches your string
Related
I am receiving date and time format as mentioned below
2016-04-13T00:12:33+05:30
i need to convert above format to HH:MM:SS AM/PM
can any one guide me,your response will be appreciated......
Android has the DateFormat class for parsing and reformatting dates as Strings, but JodaTime is much better. JodaTime is small and can handle what you're looking for in just a couple lines of code.
You need first String to Date using Simple forma tor
Then date need to change to formatResult fro expected result
SimpleDateFormat format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss", Locale.ENGLISH);
SimpleDateFormat formatResult = new SimpleDateFormat("hh:mm aa");
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
Log.i("date ====== " + formatter.format("2016-04-13T00:12:33+05:30"));
I want to get the current date day/month/year min:sec in Android so I have used the follow code
String data="";
Calendar c = Calendar.getInstance();
data=c.getTime().toGMTString();
But Eclipse notify me that the method .toGMTString(); is deprecated.
How could I get the current date as formatted String avoiding the use of this deprecated method?
From the Android documentation:
This method is deprecated.
use DateFormat
Since the GMT string is represented as 22 Jun 1999 13:02:00 GMT, we can use SimpleDateFormat (subclass of the abstract DateFormat) like so:
SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
String asGmt = df.format(c.getTime()) + " GMT";
Might want to double-check that format, but this'll get you started. Have a look at this IDEOne code.
The method toGMTString() from the type Date is deprecated.
you can check this for different types of date formations.
In your case use
SimpleDateFormat dfDate = new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss");
String data="";
Calendar c = Calendar.getInstance();
data=dfDate.format(c.getTime());
System.out.println(data);//==========> 17/Oct/2012 08:36:52
If you want to print month number instead of month name
use
SimpleDateFormat dfDate = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String data="";
Calendar c = Calendar.getInstance();
data=dfDate.format(c.getTime());
System.out.println(data);//==========> 17/10/2012 08:36:52
I know how SimpleDateFormat works, by grabbing sdfDateTime.format(new Date(System.currentTimeMillis())); which is today's date from the System. Can SimpleDateFormat format a date by grabbing a string? Let's say you had a string: String dateStr = "04/05/2012"; how would you format that into: "April 5, 2012"?
DateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy");
inputFormat.setLenient(false);
DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy");
outputFormat.setLenient(false);
String inputDateAsString = "04/05/2012";
Date inputDate = inputFormat.parse(inputDateAsString);
System.out.println(outputFormat.format(inputDate));
You can't just grab an arbitrary string and figure out what its format is.
check this one, it is very helpful library, easy to use & extend for such needs
https://bitbucket.org/dfa/strtotime/wiki/Home
I want to show the date in format 22-Apr-2012.
I am using this code:
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
sdf.applyPattern("dd MMM yyyy");
Date x = new Date(time.year,time.month,time.monthday);
System.out.println(sdf.format(x));
But i am getting o/p as:
22 apr 3912.
I want to know why it is showing 3912 in place of 2012.
Please read the api for Date class. It starts from year 1900 so in this constructor you must provide the date - 1900. But this constructor is deprecated so my advice is to start using Calendar object for your date related operations.
for Java.da
Calendar cal=Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
String dateresult = sdf.format(cal.getTime());
SimpleDateFormat dateformate = new SimpleDateFormat("dd-MMM-yyyy");
Calendar currentDate = Calendar.getInstance();
String currentDateStr = dateformate.format(currentDate.getTime());
Log.i("Infoe ", " " + currentDateStr.toString());
System.out.println("Current date is : " + currentDateStr);
////=== OR
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
sdf.applyPattern("dd-MMM-yyyy");
Date x = new Date(currentDate.get(Calendar.YEAR) - 1900,
currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH));
Log.i("Infoe ", " " + sdf.format(x));
System.out.println(sdf.format(x));
its 3912 bcz ...date is setted like...
Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).
as per GregorianCalendar
i think use of calendar is gd as date is deprecated...
nd for date format use dd-MMM-yyyy no dd-MM-yyyy bcz you want month first 2 chars...
For date Format
I am working on an XML Parser for Android with twelve different items and I need help creating a date for each item. This is what I have so far:
TextView detailsPubdate = (TextView)findViewById(R.id.detailspubdate);
I am looking to make the date look like this: Saturday, September 3. Thank you for your help!
If you are looking for todays date, you can do this by using the Date object.
Date today = new Date();//this creates a date representing this instance in time.
Then pass the date to the SimpleDateFormat.format(Date) method.
// "EEEE, MMMM, d" is the pattern to use to get your desired formatted date.
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM, d");
String formattedDate = sdf.format(today);
Finally, you can set this String into your TextView
detailsPubdate.setText(formattedDate);
Here is the documentation for SimpleDateFormat. The documentation shows the various patterns available to format Date objects.
use the DateFormat class to format your dates.
Example:
DateFormat formatter = new SimpleDateFormat("EEE, MMM d");
Date date = (Date) formatter.parse("02.47.44 PM");
detailsPubdate.setText(date.toString());
This is the java documentation for SimpleDateFormatter if you want to change your pattern.
It is not very clear what you are trying to do, but you could use the following to format date:
SimpleDateFormat sdf=new SimpleDateFormat("EEEE, MMMM, d");
//get current date
Date date=new Date();
String dateString=sdf.format(date);
You should have a look at the SimpleDateFormat class.