I am trying to convert current timestamp to DD MMM YYYY format.
but i don't know why it is giving me an error unparsable.
code:
Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date fechaNueva = format.parse(ts.toString());
System.out.println(format.format(fechaNueva));
working but I want to do
SimpleDateFormat format = new SimpleDateFormat("dd MMM YYY, HH:mm");
gives me error of unparsable.
java.text.ParseException: Unparseable date: "2014-10-07 15:38:29.876"
Following worked for me
Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date fechaNueva = format.parse(ts.toString());
format = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println(format.format(fechaNueva));
Output:
07 Oct 2014, 15:46
You were trying to parse date in a format you wanted it to be formatted instead of format in which it is already present.
Simply u can do this
Timestamp timestamp = new Timestamp(new Date().getTime());
System.out.println(timestamp);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println("Formatted "+dateFormat.format(timestamp));
hope this helps.
OutPut:-
Formatted 07 Oct 2014, 15:59
You can't use ts.toString(), you always need to use the same formatter.
Use just:
Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println(format.format(ts));
Related
If I do toString on a Date object, I am getting the output as below
2016-04-13 22:00:01.0
I am doing the below to convert the object to Data again
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd:HH:mm:SS");
Date convertedDate = (Date) formatter.parse(timestamp.toString());
But getting the ParseException.
I am trying to get the output as Date object as below
2016-04-13 22:00:01
This test should cover your example:
First from comment:
SimpleDateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy");
Date convertedDate = formatter.parse("Thu Apr 14 15:24:14 CEST 2016")
System.out.print(convertedDate);
Second from question:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date convertedDate = formatter.parse("2016-04-13 22:00:01.0");
System.out.print(convertedDate);
It all depends on input String that you are receiving.
You should not use Date::toString method, use formatter, as you willl have no problem with zones and formats. It's better to have full flow control over your data.
Now if you want to convert it to a " 2016-04-13 22:00:01" format, simply use:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatedDate = formatter.format(convertedDate);
Parse from String with a certain date-format to a Date object:
String dateString = "2016-04-13 22:00:01.0";
SimpleDateFormat formatterFullMilliseconds = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS" );
Date dateFromDateString = formatterFullMilliseconds.parse( dateString );
System.out.println( dateFromDateString ); // Output: Wed Apr 13 22:00:01 CEST 2016
Parse a java.util.Date object to the string representation you want:
SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
System.out.println( formatter.format( dateFromDateString ) ); // 2016-04-13 22:00:01
System.out.println( formatter.format( new Date() ) ); // 2016-04-14 15:29:04
yyyy-MM-dd:HH:mm:SS
should be
yyyy-MM-dd HH:mm:ss.S
This is exactly what you need to do, to escape java.text.ParseException: Unparseable date exception
Date date = new Date();
SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = sp.parse(sp.format(date));
System.out.println(dt.toString());
I want to reformat a given date String into a different format:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, MMMM dd, yyyy 'at' KK:mm aa zzzz");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = simpleDateFormat.parse(question.getOccur());
String formattedTime = output.format(d);
I'm getting this exception:
java.text.ParseException: Unparseable date: "Monday, December 7, 2015 at 12:05:13 PM Eastern Standard Time" (at offset 33)
You missed the seconds from your date format.
Try this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, MMMM dd, yyyy 'at' KK:mm:ss aa zzzz");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = simpleDateFormat.parse(question.getOccur());
String formattedTime = output.format(d);
Edit. For the following example: Thursday 12/10/2015 01:35 AM, the date format is this:
SimpleDateFormat output = new SimpleDateFormat("EEEE dd/MM/yyyy hh:mm a");
I have a String timeStamp in this format "Wed, 29 Jan 2014 20:14:15 GMT". I want to be able to compare this date with another date that is in this format 01/25/1999. I have tried simpledateformatter but with no success.
String a = connection.getHeaderField("Last-Modified"); //Returns Wed, 29 Jan 2014 20:14:15 GMT
Date lastModDate = new Date(file.lastModified()); //returns 01/25/1999
This is the simpleDateFormatter I tried implementing
SimpleDateFormat formatter;
Date dateIn = null;
formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
try{
dateIn = (Date)formatter.parse(a);
}catch(ParseException e){
e.printStackTrace();
}
Log.d(TAG, "The server date is formated to : " + dateIn);
The dateIn is always null.
I want the to be able to do something like this
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
Date strDate = sdf.parse(valid_until);
if (new Date().after(strDate)) {
}
Use the following code...you will get the problem right.
Calendar calender = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String timeStamp = formatter.format(calender.getTime());
Date strDate = formatter.parse(timeStamp);
String currentTimeStamp = formatter.format(new Date());
Date currentTime = formatter.parse(currentTimeStamp);
if (currentTime.after(strDate)) {
}
Don't know what you tried but this should work:
String a = connection.getHeaderField("Last-Modified");
Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH).parse(a);
This can help http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
The reason is that you are using the wrong date format for your formatter. If the date you receive looks like
"Wed, 29 Jan 2014 20:14:15 GMT"
Then you should use the following format
formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
You should use the Calendar class and its subclass GregorianCalendar. For exampe, to get the month of your date:
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.get(Calendar.MONTH);
I want the convert the string like 1/15/2014 9:57:03 AM or 1/15/2014 5:49:39 PM to 9:57:03 and 17:49:39
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a", Locale.US);
Date date = format.parse("1/15/2014 9:57:03 AM");
format = new SimpleDateFormat("HH:mm:ss");
String dateString = format.format(date);
BTW, use Google next time
You should see this in the doc
doc SimpleDateFormat
I am getting date from database as 2013-05-03 00:20:29.0. I want to parse the date to EEE MMM dd HH:mm:ss zzz yyyy format, but when I am parsing it, I am getting this exception:
java.text.ParseException: Unparseable date: "2013-05-03 00:20:29.0"
My code is below:
String createdDate = "2013-05-03 00:20:29.0";
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date parseDate = format.parse(createdDate);
You have to adjust the Format pattern. Format must be yyyy-MM-dd HH:mm:ss.S
Try this
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
See http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Your SimpleDateFormat pattern does not match createdDate. The format should be:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
See the javadoc
As I understod you want to convert your date to a new format
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse("2013-05-03 00:20:29.0");
String str = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").format(date);
You can also try to get the date from the DB not as String but as java.sql.Date then the first step will be unnecessary
Here is an example:
String str_date= "29/02/2013";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date dt = sdf.parse(str_date);
DateFormat df = new SimpleDateFormat("EEEE");
System.out.println(df.format(dt));