Solved

Convert date string to miliseconds timestamp

Posted on 2006-07-05
10
1,832 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Receive file in Servlet 1 36
Requested array size exceeds VM limit 3 46
recursion example 16 68
JList custom Cell Renderer refresh 15 39
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now