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

x
?
Solved

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

Posted on 2009-07-10
3
Medium Priority
?
3,630 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
[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
  • Learn & ask questions
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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: …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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.

609 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