unparseable unix timestamp - java

Unix timestamp is 1334672401.
long t = Long.parseLong(map.get("timestamp").toString());
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd, ''yy, hh:mma");
Date time = new Date(t*1000);
Date date = formatter.parse(time.toString());
I'm trying to convert a unix timestamp into a Date object in the format to something similar to Thu Apr 17 2012, 16:25 but I keep getting an unparseable date error and I'm not sure whats wrong exactly?

You have the Date object already when you did this:
Date time = new Date(t*1000);
Use the formatter to format your string output, like this:
System.out.println(formatter.format(time));

Related

DateFormatException is not thrown on February, April, June [duplicate]

I want to convert a string to date before storing it and I used
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
Date returnDate = format.parse(date);
When I ran this with sample date:
the input string for date conversion is 2014-05-06
the parsed date is Mon Jan 06 00:05:00 IST 2014
now when I store the returnDate in MySql the value is 2014-01-06 00:05:00
Why is the date changed ? Want to know if I am missing something. I went through the posts related to date string conversion : How to convert a date from a Datepicker to Mysql DATETIME format using java?
In your DateFormat use MM for month instead of mm, that is for minutes
Reference: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
You can use like this :
Date mDate= new Date(System.currentTimeMillis());
SimpleDateFormat mDateFormat= new SimpleDateFormat("dd MMM yyyy HH:mm a");
String dateformat=mDateFormat.format(mDate);
the string ["dd MMM yyyy HH:mm a"] can be changed according to need of formate.
Like in your case : "yyyy-mm-dd

Unix Timestamp gives me a date that is about 40,000 years in the future

so I get a String that is a date. Example: 2012-10-22 10:00:00 Now I want the Unix Timestamp of this date. So first I have to convert it into a SimpleDateFormat
String metaDate = (metaDateList.item(0)).getNodeValue(); //2012-10-22 10:00:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
Date nonUnixDate;
nonUnixDate = sdf.parse(metaDate);
System.out.println ( nonUnixDate.getTime() );`
The problem I'm encountering is that the unix timestamp is 1358675994000 which is Thu, 23 Sep 45024 14:20:00 GMT. Seems like I have an error somewhere, but where is it?
The unix timestamp is defined in seconds.
Date.getTime() returns milliseconds.
So your result will be off by a factor of 1000.
You are using the wrong format, you should use MM
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Convert String to Date Time Format

I have to compare 2 dates which are in String format as: Fri Aug 23 17:03:19 IST 2013
I am trying to use new SimpleDateFormat("dd-MM-yyyy HH-mm-ss") to convert in DateTime so that dates can be comparable .
tempTimeStamp=Fri Aug 23 17:03:19 IST 2013
DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
Date startDate;
startDate = df.parse(tempTimestamp);
String newDateString = df.format(startDate);
But its showing error object of this type can't be converted in DateFormat.Please help..
You were using the wrong format to try and parse the datetime string, try using the following snippet:
String tempTimeStamp="Fri Aug 23 17:03:19 IST 2013"
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date startDate = startDate = df.parse(tempTimestamp);
String newDateString = df.format(startDate);
You can look up more on the patterns for defining the date time format at http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Your expected format doesn't matches with the one provided.
The format of your SimpleDateFormat does not match that of the String that you are using.
The input String should match dd-MM-yyyy HH-mm-ss
If you are wanting simply to format the output of a Date, then no parsing of a String is necessary.

convert string to Mysql time stamp in servlet java

hi i am passing my date time string using get method in apache servlet and it goes like this
http://localhost:8084/example/Time_ser?date=15/03/2013%2004:14:30%20PM
and i am using
String time=request.getParameter("date");
to get the date value.....
and my java code to convert the string to timestamp is given below
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss aa");
java.util.Date date = (java.util.Date)formatter.parse(time);
Timestamp timets = new Timestamp(date.getTime());
but it shows error like this
java.text.ParseException: Unparseable date: "15/03/2013 04:14:30 PM"
am i doing anything wrong please help me..........
use / not - because you date is formatted as 15/03/2013 04:14:30 PM.
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");

Unparseable date in java -convert from Date into Calendar

I need to convert from Date(the object :insuree.getBirthDate()) into Calendar(the object : request_MDP.setBIRTH_DATE)
but i get the error :
21/02/13 15:20:26 ERROR [MdmInsureeService]: ServiceProxy Update
exception: 'Unparseable date: "Mon Nov 15 15:00:00 IST 1982"' 21/02/13
15:20:26 ERROR [MdmInsureeService]: ServiceProxy Update exception
(toString): 'java.text.ParseException: Unparseable date: "Mon Nov 15
15:00:00 IST 1982"
This is my code :
DateFormat formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Date date_MDP =(Date)formatter.parse(insuree.getBirthDate().toString());
Calendar cal_MDP=Calendar.getInstance();
cal_MDP.setTime(date_MDP);
request_MDP.setBIRTH_DATE(cal_MDP);
How can I convert the Date(insuree.getBirthDate()) into Calendar(setBIRTH_DATE) ?
Thanks
try this code:
SimpleDateFormat df = new SimpleDateFormat("KK:mm aa");
Date date = df.parse("10:30 PM");
System.out.print(date);
Calendar cal = GregorianCalendar.getInstance();
cal.setTimeInMillis(date.getTime());
System.out.print(cal.getTime());
Is it possible that you run this code on a system with a non-English locale? Such a locale might not recognize the weekday or month in your example date ("Mon Nov 15 15:00:00 IST 1982"), because it uses different names (e.g. "Tue" would be "Di" in German).
Replace the first line in your snippet with
DateFormat formatter =
new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy", Locale.US);
and it will work with your example date. Either way, you have to use the same format and locale that was used to generate those date strings. Maybe you can even convince the generator to use some standard format like ISO 8601.

Categories