Solved

java convert Date to UTC number

Posted on 2014-01-20
3
396 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
Comment Utility
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
Comment Utility
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
Comment Utility
?
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

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

15 Experts available now in Live!

Get 1:1 Help Now