Solved

java convert Date to UTC number

Posted on 2014-01-20
3
415 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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 …
Suggested Courses

738 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