Convert date string to miliseconds timestamp

I have a string in the format "yyyy-MM-dd'T'HH:mm:ssZ"
It will look like this 2006-07-05T23:43:12+02:00

i want this converted to a miliseconds timestamp since 1970 1. januar GMT00:00 etc..

anyone know how??
LVL 1
dreamer81Asked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
something like:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date d = sdf.parse(s);
long milli = d.getTime();

0
 
objectsCommented:
don't think u need those quotes

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ssZ");
0
 
dreamer81Author Commented:
If I dont put the quotes there i get this
java.lang.IllegalArgumentException: Illegal pattern character 'T'


If I put the quotes there i get
java.text.ParseException: Unparseable date: "2006-07-05T13:17:16+02:00"
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
girionisCommented:
Hi dreamer81

you could try

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+02:00'");
    Date d = sdf.parse(s);
    long milli = d.getTime();

Cheers
0
 
girionisCommented:
You should add 2*60*60*1000 to the result since it will leave out the 2 hours.
0
 
dreamer81Author Commented:
Hi girionis,
that seems to work, but it is not always in the second timezone?? This stuff is for a ship, and therefor the timezone is very important?
0
 
girionisConnect With a Mentor Commented:
The problem is the ":" in the timezone therefore you cannot apply the letter "Z" in the SimpleDateFormat pattern. Try the folliowing which gets rid of the lst ":"

    String s = "2006-07-05T23:43:12+02:00";
    int index = s.lastIndexOf(":");
    s = s.substring(0, index) + s.substring(index+1, s.length());
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    Date d = sdf.parse(s);
    long milli = d.getTime();
0
 
CEHJConnect With a Mentor Commented:
You can ignore the timezone info - a Date has no timezone data associated with it:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

That will rid you of your parse error
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.