?
Solved

java convert Date to UTC number

Posted on 2014-01-20
3
Medium Priority
?
438 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 1000 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 1000 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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

840 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