I spent about hour to solve problem, but I couldn't...
My date string is "06 Jan 2016", and I want to parse it to object Date.
I tried next method
SimpleDateFormat frmt2 = new SimpleDateFormat("dd MMM yyyy");
Date date = frmt2.parse("06 Jan 2016");
And I got:
java.text.ParseException: Unparseable date: "06 Jan 2016" (at offset 3)
I tried Joda lib
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd MMM yyyy");
DateTime dt = formatter.parseDateTime("06 Jan 2016");
But I got same error:
Invalid format: "06 Jan 2016" is malformed at "Jan 2016"
Can you help me please to obtain the success in this simple problem.
Thank you very much.
SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy hh:mm", Locale.ENGLISH);
Date theDate = format.parse("JAN 13,2014 09:15");
Calendar myCal = new GregorianCalendar();
myCal.setTime(theDate);
System.out.println("Day: " + myCal.get(Calendar.DAY_OF_MONTH));
System.out.println("Month: " + myCal.get(Calendar.MONTH) + 1);
System.out.println("Year: " + myCal.get(Calendar.YEAR));
Related
I am trying to parse April 18 2016 10:41 AM to 04/18/2016 10:41. Here is my code
private String dateFormat(String strCurrentDate) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("mmmm dd yyyy hh:mm Z");
Date newDate = null;
newDate = format.parse(strCurrentDate);
format = new SimpleDateFormat("MM/dd/yyyy hh:mm");
String date = format.format(newDate);
Log.d("DATE_FORMATE_TESTING", date);
return date;
}
But it gives the following errors
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err: java.text.ParseException: Unparseable date: "April 18 2016 01:41 PM" (at offset 0)
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err: at java.text.DateFormat.parse(DateFormat.java:579)
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err: at gps.clock.com.MainActivity.dateFormat(MainActivity.java:441)
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err: at gps.clock.com.MainActivity.access$500(MainActivity.java:70)
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err: at gps.clock.com.MainActivity$1$4.onClick(MainActivity.java:303)
Can anyone tell me how should I fix it? Why it is showing Unparseable date. How should I parse it?
Thanks in advance.
Try the following code
String strCurrentDate = "April 18 2016 10:41 AM";
SimpleDateFormat format = new SimpleDateFormat("MMM dd yyyy hh:mm a");
Date newDate = null;
try {
newDate = format.parse(strCurrentDate);
} catch (ParseException e) {
e.printStackTrace();
}
format = new SimpleDateFormat("MM/dd/yyyy hh:mm");
String date = format.format(newDate);
System.out.print(date);
I think you need to tweak your original format string.
SimpleDateFormat format = new SimpleDateFormat("mmmm dd yyyy hh:mm Z");
Should be
SimpleDateFormat format = new SimpleDateFormat("MMMM dd yyyy hh:mm a");
In java date formatting m and M both have different meaning. m is used to parse minutes while M is used to parse month.
In your example you are parsing month with lower case m and that causes error. As suggested by #Mike M. change your ms with upper case.
I am trying to parse a String to a Date and it giving me right date where as time is wrong.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm aaa");
try {
Date date = formatter.parse("2015-08-20 05:00 AM");
Log.e("date", date+""); // Logcat printing Thu Aug 20 00:00:00 GMT+05:30 2015
} catch (ParseException e) {
Log.e("Error",e.toString());
}
As you can see irrespective of the time every time parsed date time showing 00:00:00
What I want is Thu Aug 20 05:00:00 GMT+05:30 2015
It seems the problem was that your pattern String specified am/pm, but was using uppercase H's for the hour characters. These indicate a 24-hour clock, which obviously doesn't use am/pm. Change the hour characters to lowercase h's, which indicate the hour in am/pm (0-11).
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
The docs for SimpleDateFormat explain the various acceptable pattern characters.
Try below code
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss z");
String dateToStr = format.format(new Date());
System.out.println("dateToStr=>" + dateToStr);
try {
Date strToDate = format.parse(dateToStr);
System.out.println("strToDate=>" + strToDate);
} catch (ParseException e) {
e.printStackTrace();
}
Maybe its related to time zone issues, at what time zone is your input?, i'd suggest to make sure your paramater is on UTC timezone and then using formatter like this :
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm aaa");
formatter .setTimeZone(TimeZone.getTimeZone("UTC"));
Try this code you missed some lines of code
SimpleDateFormat mFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aa", Locale.getDefault());
mFormatter.setTimeZone(TimeZone.getTimeZone("India"));
Date startDate = mFormatter.parse("2015-08-20 05:00 AM");
This code working fine for me.
When I try to convert string to date an Exception:
String da="16-Jan-2014 10:25:00";
SimpleDateFormat sdf = new SimpleDateFormat("dd-mmm-yyyy HH:mm:SS", Locale.ENGLISH);
Date dd = sdf.parse(da);
What wrong in my code please?
java.text.ParseException: Unparseable date:"16-Jan-2014 10:25:00" (at offset 3)
thanks in advance...
You're using small letter 'm's for months. They should be changed to capital letter M's. As described here: http://developer.android.com/reference/java/text/SimpleDateFormat.html
So the pattern will be something like this:
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:SS", Locale.ENGLISH);
Consider Following Example
String text = "2014-01-17T00:00:00.000-0500";
DateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd'T'HH:mm:ss.SSSZ");
Hope this could Help
As mentioned by #Tim Kranen, the months formats are in capital letters. So it should be MMM.
To understand the months formatting consider this short code-
/*
Formatting month using SimpleDateFormat
This example shows how to format month using Java SimpleDateFormat class. Month can
be formatted in M, MM, MMM and MMMM formats.
*/
import java.text.SimpleDateFormat;
import java.util.Date;
public class FormattingMonth {
public static void main(String[] args) {
//create Date object
Date date = new Date();
//formatting month in M format like 1,2 etc
String strDateFormat = "M";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
System.out.println("Current Month in M format : " + sdf.format(date));
//formatting Month in MM format like 01, 02 etc.
strDateFormat = "MM";
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("Current Month in MM format : " + sdf.format(date));
//formatting Month in MMM format like Jan, Feb etc.
strDateFormat = "MMM";
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("Current Month in MMM format : " + sdf.format(date));
//formatting Month in MMMM format like January, February etc.
strDateFormat = "MMMM";
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("Current Month in MMMM format : " + sdf.format(date));
}
}
/*
Typical output would be
Current Month in M format : 2
Current Month in MM format : 02
Current Month in MMM format : Feb
Current Month in MMMM format : February
*
Check the Java Date Formatting with examples as mentioned above for understanding Formats.
I need to transform a date from this format : Tue Dec 10 09:00:00 EET 2013 into 3 12 2013 09:00
I've tried something like:
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm");
Date newdata = dateFormat.parse(pubDate);
SimpleDateFormat desiredDateFormat = new SimpleDateFormat("dd-MM-yyyy, hh:mm");
this.pubDate = desiredDateFormat.parse(desiredDateFormat.format(newdata));
but the date is not changing. How I can accomplish this?
Thanks!
This simple line will solve your problem
android.text.format.DateFormat dateFormat= new android.text.format.DateFormat();
dateFormat.format("dd-MM-yyyy hh:mm", new java.util.Date());
I have a date string: 02/28/2013 06:20:00 PM
I have a date formater: SimpleDateFormat dateFmt = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
java.util.Date tempDate;
when I parse the string with the formater and extract the date
tempDate = dateFmt.parse(xpp.getText());
I get a date that is off by 12 hours: Thu, Feb 28, 2013 6:20 AM
What am i overlooking?
The javadocs for SimpleDateFormat say that HH is for the hour in 24-hour format (0-23). For AM/PM format (1-12) you should use hh.