Solved

Convert String MM/dd/yyyy hh:mm:ss to Milliseconds in Java

Posted on 2009-07-10
3
3,422 Views
Last Modified: 2013-12-14
I need to know if there is a way to convert a string in MM/dd/yyyy hh:mm:ss format to its equivalent in milliseconds (Epoch time) in Java?  I have a string in this format that I need to convert to milliseconds so I can send the converted value through my program that requires the time to be in this format.  Any help would be appreciated.  
0
Comment
Question by:rkneal
3 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 24825161
You can use SimpleDateFormat to parse and get the epoach

String text = ... // Date text
Date date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss").parse(text);
long epoach = date.getTime();
0
 
LVL 17

Expert Comment

by:Thomas4019
ID: 24825162
Use the class SimpleDateFormat.
String date = "......";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
Date d = format.parse(date);
long time = d.getTime();

Open in new window

0
 

Accepted Solution

by:
rkneal earned 0 total points
ID: 25668465
I was able to resolve my issue by using the following try...catch block.  I attempt to parse the string as a long and if that fails then parse the string as a date.

 Date localDate = new Date();
        try
        {
          localDate.setTime(Long.parseLong(paramString));
        }
        catch (NumberFormatException localNumberFormatException)
        {
          localDate.setTime(Date.parse(paramString));
        }
0

Featured Post

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.

Question has a verified solution.

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

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

856 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