-4
long beginupd = new GregorianCalendar(2014,3,14,10,55,25).getTime().getTime();              
Date date = new Date();
long milli=date.getTime();           

System.out.println(beginupd);
System.out.println(milli);
System.out.println(date);

output:

1397462125000

1394787327009

Fri Mar 14 10:55:27 EET 2014

What is my wrong? why is it not equal? difference onyl two second but output difference very large

OK! 0 for January and 11 for December. thank you David Wallace

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2772836
  • 79
  • 1
  • 2
  • 9
  • 3
    What format is the input in? Is it a `String`? Is it `Date` value? – MadProgrammer Mar 13 '14 at 23:41
  • 3
    YES! If you have a `String` value, you need parse it back to a `Date` so you can get the milliseconds. If you already have a `Date` value, then you don't need to do the parsing...I also assume you're trying to get the duration between these two values...Which can be seen [here](http://stackoverflow.com/questions/18119665/geting-duration-of-2-times/18120233#18120233) and [here](http://stackoverflow.com/questions/12851934/how-to-find-difference-between-two-joda-time-datetimes-in-minutes/12852021#12852021) – MadProgrammer Mar 13 '14 at 23:45
  • @user2772836 Parse it into a `Date` then use [`Date.getTime()`](http://docs.oracle.com/javase/7/docs/api/java/util/Date.html#getTime()). The date format is arbitrary as long as you can construct an appropriate [`SimpleDateFormat`](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) to represent it. – Jason C Mar 13 '14 at 23:46
  • Parse it to a date using `SimpleDateFormat` and call the `getTime()` method. That should probably do it. – Steinar Mar 13 '14 at 23:46
  • possible duplicate of [Convert UTC date into milliseconds JAVA](http://stackoverflow.com/questions/12081417/convert-utc-date-into-milliseconds-java) – Jason C Mar 13 '14 at 23:51
  • MadProgrammer if I say that I want to calculate 3+5, will you say that it is int or double? it is doesn't matter. – user2772836 Mar 13 '14 at 23:54
  • @user2772836 Please keep comment discussions directly related to the problem at hand. – Jason C Mar 14 '14 at 00:03
  • What on earth does `2014-14-03` mean? Did the year just get longer? – Dawood ibn Kareem Mar 14 '14 at 00:08
  • @DavidWallace yyyy-dd-MM I presume. A little odd. – Jason C Mar 14 '14 at 00:08

3 Answers3

2

If it is not already a Date, parse it into a Date. The date format is arbitrary as long as you can construct an appropriate SimpleDateFormat to represent it.

After you have a Date, you can use Date.getTime() to retrieve the millisecond value.

For the example you have shown, if you have a string:

String datestr = "2014-14-03 01:39:00";

Then the matching SimpleDateFormat would be:

DateFormat format = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");

And conversion would be:

long millis = format.parse(datestr).getTime();

It's no problem to use Date for this, as the constructors and getTime() are still some of the few remaining non-deprecated components.


Edit: I see that you have edited your question to include the use of Date. The constructor you are using is deprecated, and is also not very flexible wrt. input (you have to have the date components already parsed to use it). A SimpleDateFormat provides a non-deprecated way to convert arbitrary strings to dates.

Jason C
  • 38,729
  • 14
  • 126
  • 182
1

The reason this doesn't work is because the deprecated Date constructor that you're using expects year - 1900 as the first argument.

You should either use a SimpleDateFormat or a GregorianCalendar to do this conversion instead. Since there is already an excellent answer here, showing the use of SimpleDateFormat, here's how you use GregorianCalendar for 1:39am on 14 March 2014.

new GregorianCalendar(2014, 2, 14, 1, 39, 0).getTime().getTime();

Beware that the month uses 0 for January and 11 for December.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0

There is a nice article on the Date APIs that can be found here. http://www.mkyong.com/java/java-time-elapsed-in-days-hours-minutes-seconds/

In order to convert to milliseconds, simply do some basic math.

Clement Hoang
  • 675
  • 9
  • 18
  • The math to convert a date to milliseconds is far from basic (consider leap time, etc. -- also timezone conversions if not in UTC). There's no need to do the math by hand as all available date APIs already provide a facility to convert to millisecond epoch time. – Jason C Mar 14 '14 at 00:07