Solved

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

Posted on 2009-07-10
3
3,319 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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. …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

771 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

12 Experts available now in Live!

Get 1:1 Help Now