Solved

java convert Date to UTC number

Posted on 2014-01-20
3
420 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
[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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

726 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