Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Convert date string to miliseconds timestamp

Posted on 2006-07-05
10
1,836 Views
Last Modified: 2011-10-03
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??
0
Comment
Question by:dreamer81
  • 3
  • 2
  • 2
  • +1
10 Comments
 
LVL 92

Accepted Solution

by:
objects earned 168 total points
ID: 17041503
something like:

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

0
 
LVL 92

Expert Comment

by:objects
ID: 17041505
don't think u need those quotes

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ssZ");
0
 
LVL 1

Author Comment

by:dreamer81
ID: 17041554
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 35

Expert Comment

by:girionis
ID: 17041833
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
 
LVL 35

Expert Comment

by:girionis
ID: 17041866
You should add 2*60*60*1000 to the result since it will leave out the 2 hours.
0
 
LVL 1

Author Comment

by:dreamer81
ID: 17042021
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
 
LVL 35

Assisted Solution

by:girionis
girionis earned 166 total points
ID: 17042108
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 166 total points
ID: 17044250
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Custom Java Application connects to database: Which one of the 20? 4 35
jboss wildfly 10.1 10 228
mysql jsp example issue 32 48
printf performancy 11 68
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question