Solved

java convert Date to UTC number

Posted on 2014-01-20
3
402 Views
Last Modified: 2014-02-04
i have a date string : 2010-04-16T00:00:00.000000
i want to conver to this UTC string: 1265241600000

can someone tell me how to do that?  thanks.
0
Comment
Question by:mmingfeilam
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
Surrano earned 250 total points
ID: 39796224
Date.getTime() returns milliseconds from Jan 1, 1970 till given date.
Calendar.getTime() does the same.
System.currentTimeMillis() returns milliseconds till now.

So once you've parsed the input string into a Date you just invoke the getTime() method.
The tricky part is that there are no constructors / methods where you can specify milliseconds for the Date object except by using the format that is actually your desired output.

  String indate="2010-04-16T00:00:00.000000";
  String[] parts = indate.split("[-T:.]");
  int y = Integer.parseInt(parts[0]);
  int mo = Integer.parseInt(parts[1]);
  int d = Integer.parseInt(parts[2]);
  int h = Integer.parseInt(parts[3]);
  int mi = Integer.parseInt(parts[4]);
  int s = Integer.parseInt(parts[5]);
  int ms = Integer.parseInt(parts[6]);
  Calendar c = new GregorianCalendar(y,mo,d,h,mi,s);
  long l = c.getTime() + ms;

Open in new window

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 39796613
i want to conver to this UTC string: 1265241600000
I think you'll find that should be 1271376000000

    public static String timestampToMilliseconds(String timestamp)
        throws ParseException {
        final String TS_FORMAT = "yyyy-MM-dd'T'hh:mm:ss.S";
        Date d = new SimpleDateFormat(TS_FORMAT).parse(timestamp);
        return Long.toString(d.getTime());
    }

Open in new window

(The parsing stuff uses the java.text.* package)

If you want to remove any room for error caused by incorrect setting of the timezone, you should probably do

 
   public static String timestampToMillisecondsUTC(String timestamp)
        throws ParseException {
	
        final String TS_FORMAT = "yyyy-MM-dd'T'hh:mm:ss.S";
        DateFormat df = new SimpleDateFormat(TS_FORMAT);
	df.setTimeZone(TimeZone.getTimeZone("UTC"));
	Date d = df.parse(timestamp);
        return Long.toString(d.getTime());
    }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39832083
?
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

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 This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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.

914 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

14 Experts available now in Live!

Get 1:1 Help Now