java.text.ParseException: Unparseable date: "531772200000" - java

I need to convert the string 531772200000 to a Java Date object. The date is stored in a database.
When I am doing this, I am getting java.text.ParseException: Unparseable date: "531772200000".
My code:
String dateToBeConverted = String.valueOf(dbObject.get("customerDateOfBirth"));
String parseabledate = dateToBeConverted
.replace("/Date(","")
.replace(")/","")
.replace("+0530", "");
dbObject.put("_id", String.valueOf(dbObject.get("userInfoId")));
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date date;
date = formatter.parse(parseabledate);

This looks like a timestamp value, this will probably give you the date:
new Date(Long.parseLong("531772200000"));
which works out at Fri Nov 07 1986 18:30:00 GMT+0000

Here is one solution that will provide the date correctly formatted.
String d = "531772200000";
SimpleDateFormat newFormatter = new SimpleDateFormat("dd-MMM-yyyy");
try {
Date date1 = new Date(Long.parseLong(d));
System.out.println(newFormatter.format(date1)); //Will print out as 07-Nov-1986
} catch (ParseException e) {
e.printStackTrace();
}
Another solution is to use Joda Time with a solution below.
String d = "531772200000";
DateTime newDate = new DateTime(Long.parseLong(d));
DateTimeFormatter dd = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
DateTime date = dd.parseDateTime(newDate.toString());
System.out.println(date.toString("dd-MMM-yyyy")); //Prints out as 07-Nov-1986
Personally I prefer to use the second solution (Joda Time) as it is much nicer and easier.

It is epoh time format.
Somebody already answered about that in https://stackoverflow.com/a/20732668/379779
and test your epoh time in this website http://www.epochconverter.com/

Related

Fast way to parse a Long date into Month-Day-Year String

How do you parse a Long date like: 1366222239935 into a String of space-separated Month-Day-Year? Like into "Apr 18 2013"
Passing it on a java.util.Date and to a String will give a String of date which contains so many info that I don't need for rendering in my GWT application.
Need to do this style since I will be passing the result into 3 <span> elements; so actually the space-separated date will be split into parts:
Month
Day
Year
As gwt won't support SimpleDateFormat
instead use Gwt DateTimeFormat
DateTimeFormat f = DateTimeFormat.getFormat("dd-MMM-yyyy");
String datestring =f.format(dateGeneratedbyLong);
And make sure the DateTimeFormat import also which you can use both client and server side .
There is another class with same name but package is different which is client(restricts you to use on client side only )
try to use SimpleDataFormat check http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html>
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yy");
String dateAsString = simpleDateFormat.format(new Date());
You could convert it to a Date and then manually build your String like this.-
Date date = new Date(timeInMils);
String res = date.get(Calendar.MONTH) + " " +
date.get(Calendar.DAY_OF_MONTH) + " " +
date.get(Calendar.YEAR);
That Long number is simply the number of milliseconds since the JavaScript epoch (1/1/1970 at midnight, UTC time). So, instead of parsing it, use the constructor for the Date object:
var myDate = new Date(1366222239935);
alert(myDate);
That will show "Wed Apr 17 2013 11:10:39 GMT-0700 (Pacific Daylight Time)". I am in PST, but it will default to whatever timezone you have in your locale settings.
Inside a GWT app in Java, simply do:
Date date=new Date(1366222239935);
Then you can use SimpleDateFormat to render it as "dd/MM/yy".
See http://www.w3schools.com/jsref/jsref_obj_date.asp
Do like this
Date d = new Date();
d.setTime(1366222239935l);
System.out.println(d);
SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd yyyy");
try {
System.out.println(sdf.format((d)));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long diff = 1366222239935l;
Date date = new Date(diff);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
System.out.println(sdf.format(date));

How to format my date with SimpleDateFormat?

I'm trying to set a date format, but when i run this code
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
Date date = new SimpleDateFormat("yyyy-mm-dd").parse(oldstring);
System.out.println("datefield = "+date);
i take result:
oldstring = 2013-01-1
datefield = Tue Jan 01 00:01:00 MSK 2013
Why datefield isn't equal 2013-01-1?
At first mm in yyyy-mm-dd mean minute not Month. to set month use MM.
It would be look like this :
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(oldstring);
UPDATE
Try this:
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
Date date = new SimpleDateFormat("yyyy-mm-dd").parse(oldstring);
String sdf = new SimpleDateFormat("yyyy-mm-dd").format(date);
System.out.println("datefield = "+sdf);
If you don't use new SimpleDateFormat("yyyy-mm-dd").format(date);
you getting standard date format which include all info. If you want special format you need to use
new SimpleDateFormat("yyyy-mm-dd").format(date);
Also read this article about date formatting
The type of datefield is Date, so the toString method will basically always return the same format, as you are not overriding it.
So what you need to do, is basically:
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(oldstring);
System.out.println("datefield = "+date);
String outDateStr = sdf.format(date);
System.out.println("newstring = "+outDateStr);
Use MM for month. mm is for minutes

change date format to query database

how do I change date format of the following date
Thu May 17 00:00:00 GMT+05:30 2012
to
2012-05-17 00:00:00
I need it as date and not as string. I am using
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("")
But its not giving the result.
Actually I am storing the values date wise. So when someone enters the data for the same date again it should overwrite.
If I pass it as date object into the hibernate query it gives the result. But not always. ON few occasions it inserts again that is it inserts duplicate data for the same date. Befroe entering I am adding a method to check if data exists for that date. criteria.add(Restrictions.eq("date", date));
You shouldn't be doing string manipulation at all here. You've said in comments that it's a date/time field in the database, so why would there be any string conversion involved in your code?
Specify parameters in JDBC as java.sql.Date, java.sql.Timestamp or whatever - and then fetch them that way too. Don't do a string conversion. Ignore whatever format happens to be displayed when you query the database in a tool - don't think of the result as having a "format" at all - they're just dates.
String str ="Thu May 17 00:00:00 GMT+05:30 2012";
DateFormat old_format = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
Date newDate = null;
try {
newDate = old_format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
DateFormat new_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = new_format.format(newDate);
System.out.println("==>"+date);
Do exactly as you do now but change parse("") to format(theDate)
theDate being the Date object you want to format
See
http://docs.oracle.com/javase/6/docs/api/java/text/DateFormat.html#format(java.util.Date)
String date = "Thu May 17 00:00:00 GMT+05:30 2012";
String oldFormat = "EEE MMM dd hh:mm:ss z yyyy";
String newFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf1 = new SimpleDateFormat(oldFormat);
SimpleDateFormat sdf2 = new SimpleDateFormat(newFormat);
sdf2.format(sdf1.parse(date))

Date formatting issue in java

When I parsing time in java, I passing "12:12" as string argument, then I am getting "Thu Jan 01 12:12:00 IST 1970" as a output.
I want current year like "Fri Mar 09 12:12:00 IST 2012" as a output.
String timestr = "12:12";
Date convertedDate = null;
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
convertedDate = dateFormat.parse(timestr);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(convertedDate);
Thanks!
I think that the problem with this is that you are creating a date with null values and then just initialize the time value. I think you should use the Calendar class and get an instance of the Calendar and then set the time. Once that is done, you create a date object from the Calendar and parse it to your needs.

How do I parse a standard date into GMT?

Can someone show me a piece of java code that parses this date:
2009-08-05
INTO THIS GMT DATE:
2009/217:00:00
====
what i have so far is:
java.text.SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
java.util.Calendar cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"));
format.setCalendar(cal);
java.util.Date date = format.parse(sdate);
but its not working
Here is the format you're looking for:
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2009-08-05");
String parsedDate = new SimpleDateFormat("yyyy/D:HH:mm").format(date);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
That's how to set it to GMT at least. Not sure where you are getting 2009/217 from 2009-08-05
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("dd:MM:yyyy HH:mm:ss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(dateFormatGmt.format(new Date())+"");
This will convert your local time to GMT.

Categories

Resources