2

I am trying to convert millseconds to time in Java.

when I do this in C#

DateTime EpochOrigin = new DateTime(1970, 1, 1, 0, 0, 0, 0);

Console.WriteLine("1406205185123 = " + EpochOrigin.AddMilliseconds(1406205185123));

result 24/07/2014 12:33:05

when I do same in Java

 Calendar cc = new GregorianCalendar();

cc.setTimeInMillis(1406205185123L);

result Thu Jul 24 13:33:05 BST 2014

The Java result add 1 more hour than C# .

Any suggestion how can I fix this?

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
user3644708
  • 2,466
  • 2
  • 16
  • 32
  • 2
    That's a timezone issue, as you see, Java gives you a BST time, ie British Time, ie UTC+1. C# gives you the result in UTC timezone. – OlivierH Jul 24 '14 at 16:00
  • @OlivierH That should have been your answer. – Measurity Jul 24 '14 at 16:09
  • @OlivierH thanks for your answer. C# did not us UTC time as well. the reason is I need to do this Calendar cal = Calendar.getInstance(); cal.set(1970, 0, 1, 0, 0, 0); cal.set(Calendar.MILLISECOND, 0); – user3644708 Aug 01 '14 at 21:40

3 Answers3

2

As you can see result in Java has BST mark which tells that it is in British summer time. So GregorianCalendar takes your time zone into account.

DateTime in C# is not aware of time zone so it is UTC because unix epoch is UTC and if you add miliseconds to UTC time you will get UTC time as well.

Mateusz
  • 2,287
  • 20
  • 28
2

In C# DateTime doesn't store time zone information, but has a Kind property whose value indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neithee (see MSDN). The default value of DateTime.Kind property is Unspecified. Therefore, in your C# code, a DateTime structure created with the line

DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);

should definitely be created in this way:

DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

in order to be used as the reference time for the conversion (Epoch time origin is January 1, 1970, 00:00 UTC).

More information can be found in this previous question: https://stackoverflow.com/a/2883645/1236452.

Edit 1

A quick remark: DateTime in C# is somehow aware of the time zone (my local time is GMT+1)(edit: please refer to comments and Edit 2):

DateTime t = new DateTime(1970,1,1,0,0,0);
Console.WriteLine(t.ToLocalTime());      // 01/01/1970 01:00:00 (GMT+1)
Console.WriteLine(t.ToUniversalTime());  // 31/12/1969 23:00:00 (GMT)

Edit 2

As correctly pointed out in the comments, DateTime is not aware of the time zone in the sense that it holds the time zone information. However, in its Kind property it does store a value that indicates whether the instance is based on local time or UTC, as stated in the documentation:

DateTime.Kind Property: Gets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither. [...]

The Kind property allows a DateTime value to clearly reflect either Coordinated Universal Time (UTC) or the local time. In contrast, the DateTimeOffset structure can unambiguously reflect any time in any time zone as a single point in time.

Community
  • 1
  • 1
lmillefiori
  • 486
  • 3
  • 9
  • `DateTime in C# is somehow aware of the time zone (my local time is GMT+1):` You are wrong date time is not aware, if kind is not specified. It is `ToUniversalTime()` method that is aware of time zone. When you call it, you see that it is substracting one hour if you convert epoch to universal time. – Mateusz Jul 24 '14 at 18:48
  • @Mateusz Apologies if my words were misleading but I said that it `DateTime` is _somehow_ aware of the time zone. Specifically, `ToUniversalTime()` method makes use of the `Kind` property of the `DateTime` object, as clearly stated in [MSDN](http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime(v=vs.110).aspx): `Starting with the .NET Framework version 2.0, the value returned by the ToUniversalTime method is determined by the Kind property of the current DateTime object.` – lmillefiori Jul 25 '14 at 07:49
0

thank you for your replies and answers. I find out why:

both C# and Java aware of the time zone.

because c# did this

DateTime EpochOrigin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
EpochOrigin.AddMilliseconds(1406205185123)

in Java, it should be

Calendar cal = Calendar.getInstance();
cal.set(1970, 0, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
long result = cal.getTimeInMillis();
long value = result + 1406205185123;
return new Timestamp(value);
user3644708
  • 2,466
  • 2
  • 16
  • 32