Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Convert date string to miliseconds timestamp

Posted on 2006-07-05
Medium Priority
1,844 Views
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
Question by:dreamer81
[X]
###### 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
• 3
• 2
• 2
• +1

LVL 92

Accepted Solution

objects earned 672 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

ID: 17041505
don't think u need those quotes

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

LVL 1

Author Comment

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

LVL 35

Expert Comment

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

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

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

girionis earned 664 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

CEHJ earned 664 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

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done withâ€¦
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to aâ€¦
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a vâ€¦
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
###### Suggested Courses
Course of the Month8 days, 22 hours left to enroll