I am using SimpleDateFormat class to parse date as follows :
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
But, getting the Parse exception error as follows :
java.text.ParseException: Unparseable date: "2015-09-16 10:21 AM" (at offset 16)
I have checked another format as a argument in SimpleDateFormat class is :
"yyyy-MM-dd HH:mm:ss a" but, still same error is coming.
Is there any other datetime format I have to pass as a argument of SimpleDateFormat class ?
As you have said in the above comments, you use a TimePicker for choosing a date time and parse it. And you said that you also want the second. But apparently, TimePicker doesn't include the second. So how are you going to know what second the user wants? I guess 0. You can insert a string into the correct position to make it work. In you case you should insert ":00" in index 16. Just use a StringBuilder!
StringBuilder builder = new StringBuilder("2015-09-16 10:21 AM");
builder.insert (16, ":00");
Try this if your input has not second value ..
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
And if it has second value also then use this
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
otherwise above one
For More detail....
TimePicker widget doesn't provide year-month-day , it only provide hour and minutes
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
To get year-month-day you have to use DatePicker widget
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"));
In my app i need to convert my current date into UTC format, i can successfully converted, now problem is i need 24 hours format check it out my below code
public static String CurrentDate() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
LogUtil.d("new Date()" + new Date());
return df.format(new Date());
}
now my CurrentDate returns 1.45 but i need 13.45 how can i convert utc in 24 hours format?
i have search through google but dint get proper answer, all suggestions are most welcome
Thanks in advance
Changing the hour part at your SimpleDateFormat constructor call from hh to HH does the job:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Output:
2015-07-13 13:53:02
See also Table of Date and Time Patterns
As Jon Skeet said, check the String you pass on SimpleDateFormat() : http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You need to replace "hh" with "HH" so it will become "yyyy-MM-dd HH:mm:ss"
Of course if you only need the hour and minute that should be "HH:mm"
hi I have the method below which takes the value of a UTC datetime string, format it to local display and return:
public static String convertDateStringUTCToLocal(String sourceUtcDateTimeString)
{
SimpleDateFormat simpleDataFormat = new SimpleDateFormat();
simpleDataFormat.setTimeZone(getCurrentTimeZone());
String outputUTCDateTimeString = simpleDataFormat.parse(sourceUtcDateTimeString, new ParsePosition(0)).toString();
return outputUTCDateTimeString;
}
public static TimeZone getCurrentTimeZone()
{
Calendar calendar = Calendar.getInstance();
TimeZone outputTimeZone = calendar.getTimeZone();
return outputTimeZone;
}
I use getCurrentTimeZone() because user may change their local setting anytime and I don't want to hard code the format.
While debugging, the value of parameter sourceUtcDateTimeString is 'Mon Apr 15 13:54:00 GMT 2013', I found that 'simpleDataFormat.parse(sourceUtcDateTimeString, new ParsePosition(0))' gives me 'null', and 'simpleDataFormat.parse(sourceUtcDateTimeString, new ParsePosition(0)).toString()' throws error "java.lang.NullPointerException at toString()".
Looks like there is nothing at ParsePosition(0), but I am really new to Android dev, no idea why this is happening and how to get around it, could any one help out with a fix? I got stuck on this issue for hours.
thanks in advance.
It looks like the string you are trying to parse comes from a Date's toString() method, which gives the format dow mon dd hh:mm:ss zzz yyyy (see the javadoc). To parse that back to a Date you can either use Date parsed = new Date(Date.parse()), which is deprecated, or use a SimpleDateFormat with the format EEE MMM dd HH:mm:ss zzz yyyy (see SimpleDateFormat Documentation).
For example this code works for me:
public static String convertDateStringUTCToLocal(String sourceUtcDateTimeString)
{
SimpleDateFormat simpleDataFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
// SimpleDateFormat already uses the default time zone, no need to set it again
String outputUTCDateTimeString = simpleDataFormat.parse(sourceUtcDateTimeString, new ParsePosition(0)).toString();
return outputUTCDateTimeString;
}
Depending on the rest of you application you should consider passing around any dates as a Date instance rather than using a String. You can then simply apply the right format any time you need to display the date to the user.
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.
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