Take a look at my code:
try {
// String date = "30Jul2013";
SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH);
Date d = sdf.parse(date);
SimpleDateFormat nsdf = new SimpleDateFormat("MMMM dd, yyyy h:mm a", Locale.ENGLISH);
String nd = nsdf.format(d);
System.out.println(nd);
return nd;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Im am getting a error:
java.text.ParseException: Unparseable date: "2013-07-30 10:58:55.171"
at java.text.DateFormat.parse(DateFormat.java:337)
I would like to have an output of July 30, 2013 11:10 AM from the simpledateformat. There's LOCALE in my code. So what else should I do?
Thanks in advance!
try {
// String date = "30Jul2013";
SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH);
Date d = sdf.parse(date);
Your date String variable line is commented out, so who's to know what String you're parsing? -- the JVM that's who.
As Robert Harvey points out, the String that you're actually trying to parse is printed for you in the exception message. If you print that String before you parse you'll also see that it's not what you expect it is and that the compiler's right.
In sum, you are somehow expecting that your sdf SimpleDateFormat object is formatting a String of a format similar to "30Jul2013", but the JVM is telling you that this simply is not so, that the String you are trying to parse in fact looks nothing like this, but rather is "2013-07-30 10:58:55.171".
Related
This question already has answers here:
Changing String date format
(4 answers)
Closed 4 years ago.
I'm trying to convert a string to date but every time i do it keeps throwing errors at me, Im not sure what i am missing
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
try {
// m brings in all variables from a get/setter
date = format.parse(m.getEventTime());
} catch (ParseException e) {
e.printStackTrace();
Log.d("Response.Error.Date", m.getEventTime());
}
eventTime.setText(date.toString());
My variable m.getEventTime() is passing the following string 2018-04-28 14:00:00
I have tried passing the string as 2018-04-28T14:00:00Z to no avail.
No errors are coming from the stack trace from the try/catch block but the log is printing out D/Response.Error.Date: 2017-08-19 15:00:00
when i add e.toString() to the log it prints out D/Response.Error.Date: 2017-08-19 15:00:00 java.text.ParseException: Unparseable date: "2017-08-19 15:00:00"
On the actual application when run the time is shown as now Sat Apr 28 08:22:33 GMT+01:00 2018
Am i missing something?
You can try this,
Date date = new Date();
SimpleDateFormat oldformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
try {
// m brings in all variables from a get/setter
date = oldformat.parse(m.getEventTime());
} catch (ParseException e) {
e.printStackTrace();
Log.d("Response.Error.Date", m.getEventTime());
}
eventTime.setText(format.format(date));
You can get time format like this:
String dateTime = null;
DateFormat df = new SimpleDateFormat("dd-MM-yyyy, hh:mm a");
//here you can use your required format
dateTime = df.format(new Date(stringTime));
You are converting date into English which is causing the exception:
Please Change:
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
eventTime.setText(date.toString());
with
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a");
eventTime.setText(format.format(date));
I want to validate the code of Date.
Input come from Textbox where user enters it.
and in code it will get calender's date instance and match it.
I want to put character in that SimpleDateFormat.
Code :
SimpleDateFormat formatter = new SimpleDateFormat("dd'ch' MMM, yyyy");
System.out.println("Date is :: " + formatter.format(Calendar.getInstance().getTime()));
String input = "20th Mar, 2014";
if(input.equals(formatter.format(Calendar.getInstance().getTime()))){
System.out.println("Matched");
}else{
System.out.println("Not Matched");
}
I want to put th, rd, st on place of ch in SDF.
means it will take input from user so it can be any date so I want some mechanism so only three will be placed at there.
Anyone knows that how can I do this ?
Help..
UPDATE
SimpleDateFormat formatterth = new SimpleDateFormat("dd'th' MMM, yyyy");
SimpleDateFormat formatterrd = new SimpleDateFormat("dd'rd' MMM, yyyy");
SimpleDateFormat formatternd = new SimpleDateFormat("dd'nd' MMM, yyyy");
SimpleDateFormat formatterst = new SimpleDateFormat("dd'st' MMM, yyyy");
String input = "20th Mar, 2014";
String input1 = "23rd Mar, 2014";
try {
if(input.equals(formatterth.parse(input1)) || input.equals(formatterrd.parse(input1)) || input.equals(formatternd.parse(input1)) || input.equals(formatterst.parse(input1))){
System.out.println("Matched");
}else{
System.out.println("Not Matched");
}
} catch (ParseException e) {
e.printStackTrace();
}
You can't, basically. You need three separate SimpleDateFormat objects:
new SimpleDateFormat("dd'st' MMM, yyyy")
new SimpleDateFormat("dd'nd' MMM, yyyy")
new SimpleDateFormat("dd'th' MMM, yyyy")
... then try parsing with each of them. Note that even this only works with ordinals in English... and it will parse "20st Mar, 2014" which possibly it shouldn't.
Ordinals in date formats are fundamentally a pain, and I haven't personally seen any API which deals with them nicely - partly because they're a pain in localization in general.
You can not do that with one Simple date format but you have to get Day from date check what kind of format you want to use.
Calendar cal = Calendar.getInstance();
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
SimpleDateFormat formatter;
if(dayOfMonth==1||dayOfMonth==21||dayOfMonth==31)
formatter = new SimpleDateFormat("dd'st'MMM, yyyy");
if dayOfMonth like 1,21,31 will use "dd'st' MMM YYYY" and so on.
One other way is try this...
String str="'th'";
String s="dd"+str+"MMM, yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(str);
You can change str="'st'" and str="'rd'"
One more way come to my mind is By the use of method you just need ONE SimpleDate format
public String Method(String str)//pass"''th","'st'" or "'nd'"
{
SimpleDateFormat formatter = new SimpleDateFormat("dd"+str+"MMM, yyyy");
return formatter.format(Calendar.getInstance().getTime()).toString();
}
Try this method(ANS for your UPDATED Question):
public boolean CheckForST(String yourdate)
{
try {
SimpleDateFormat formatter2 = new SimpleDateFormat("dd'st' MMM, yyyy");
formatter2.parseObject(yourdate);
return true;
} catch (ParseException e) {
return false;
}
}
Which is only implemented for 'st' you can do the same for 'rd' or 'nd'
I have a date in the format:Thu- Mar 22 2012.Its is obtained in a string variable date1.I need to convert the date in string variable to date format.I tried the below ccode but failed to parse the date;Please help.
DateFormat formatter;
Date formatted_date= null;
formatter = new SimpleDateFormat("EEE-MM d yyyy");
try {
formatted_date= (Date) formatter.parse(date1);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You need three Ms 'MMM' to parse a months abbreviation like 'Mar'. And please check the whitespace after 'EEE-'.
Change it to following format
EEE- MMM d yyyy
Note space after - and MMM
Thu- Mar 22 2012
EEE- MMM dd yyyy
I think you need something like this
UPD: for date formatting:
SimpleDateFormat toStringFormatter = new SimpleDateFormat("EEE MMM dd yyyy");
String formattedDate = toStringFormatter.format(date);
so parse() is for String -> Date, and format() is for Date -> String
If the already posted answers doesn't work then you surely have a problem with the locale. Try the following SimpleDateFormat:
formatter = new SimpleDateFormat("EEE- MMM d yyyy", Locale.ENGLISH);
I thank With the given mode and given the locale's default date format symbols constructor SimpleDateFormat. Note: This constructor may not support all locales. To cover all the language environment
public SimpleDateFormat(String pattern,Locale locale)
//local:Locale.ENGLISH
//default is not English
formatter = new SimpleDateFormat("EEE, MMM d, ''yy", Locale.ENGLISH);
try {
formatted_date= (Date) formatter.parse("Wed, Jul 4, '01");
} catch (ParseException e) {
e.printStackTrace();
}
I am trying to add a new pattern to the date display but I am not getting the result that I am expecting:
Here is my code:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
sdf.applyPattern("yyyy-MM-dd");
Date date_out = null;
try {
date_out = sdf.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
((TextView) view.findViewById(R.id.date)).setText(date_out.toString());
I want the output to look something like this: 03 Oct 2011
However this is the output tat I am getting: Oct 03 00:00:00 GMT+ 11:00 2011
How do I reach the desired output?
EDIT:
I solved this code by adding this line:
sdf.format(date_out);
instead of setText()
Date.toString(); does always format your String that way. You should a SimpleDateFormat to format the Date object to the String you want.
The JavaDoc of the Date.toString(); method says:
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
You have to use two SimpleDateFormat objects. One for parsing the input and an other one for formatting the parsed Date object to String.
final String inputDate = "2011-05-08T11:12:13.0123";
final SimpleDateFormat inputParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
Date date_out = null;
try {
date_out = inputParser.parse(inputDate);
} catch (final ParseException e) {
e.printStackTrace();
}
final SimpleDateFormat outputFormatter =
new SimpleDateFormat("dd MMM yyyy", Locale.US);
final String result = outputFormatter.format(date_out);
System.out.println(result);
Output:
08 May 2011
The Date.toString() method formats the string like that (check the api documentation).
You do not actually need to use applyFormat(...) in this case. You want to parse one format and output it in another format.
To parse the date (given the string: 2011-10-03") use can use the format"yyyy-MM-dd"and when you output theDateyou want to use"dd MMM yyyy"`:
public static void main(String[] args) throws ParseException {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy MMM dd");
Date parsedDate = inputFormat.parse("2011-10-05");
System.out.println(outputFormat.format(parsedDate));
}
Outputs (on US locale):
2011 Oct 05
Read below document :-
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
hope help u above link.
I am developing an application and I am stuck in converting string like 01/01/2037 01:00:00 AM
to Date
I used
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh.mm.ss.S aa")
Date d = dateFormat.parse(dateString);
but I get an error, any help will be appreciated.
you are converting this 01/01/2037 01:00:00 AM
therefore use
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa")
(more info in [documentation])1
then
Date date = dateFormat.parse("01/01/2037 01:00:00 AM");
keep in mind you have to wrap a try-catch around the parse method.
The problem is that the format you declared is nothing like the String you are trying to parse:
your String uses / to separate day, month, year while in your formatter you use -
your string separates hours with a dot, while in the formatter you use :
you do not have milliseconds in your string while you declared them in the formatter.
The following code should work:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh.mm.ss.S aa");
try {
Date date = dateFormat.parse("01-01-2037 01.00.00.000 AM");
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}