Solved

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

Posted on 2009-07-10
3
3,363 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse error 7 92
Notepad++ Remote File Management 3 79
Breakpoint problem 8 82
Copying WordPress Pages 5 76
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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

912 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

24 Experts available now in Live!

Get 1:1 Help Now