-5

I need to get current time in UNIX format. But with System.currentTimeMillis() i get time, which is not time of my timezone. (My timezone is "GMT+3")

I want to convert my local time to UNIX format. How can i do that in Android?

I want to find simple and short way of it.

P.S.: In Android i can't use LocalDateTime.

Michael Abyzov
  • 452
  • 1
  • 6
  • 16
  • What about Joda? Can you use that? – Joe C Oct 08 '17 at 09:33
  • @Zoe it's not duplicate. So explain me how can i get unix time from SimpleDateFormat. Your duplicate doesn't contain my case. – Michael Abyzov Oct 08 '17 at 09:43
  • 2
    UNIX time does not have time zones. It is the same everywhere on the world. – Henry Oct 08 '17 at 09:45
  • @JoeC i'm beginner in the android development and don't know all ways to do it. I think, i can use it. Can u explain me this way in more details? – Michael Abyzov Oct 08 '17 at 09:45
  • Of course it doesn't contain your **exact** case. No questions ever do unless you ask it yourself. It is a duplicate because it can be changed (with small changes) to handle your case. – Zoe Oct 08 '17 at 09:46
  • If you're asking me to write it for you, that's not going to happen. I suggest you do some research, make an attempt, and come back with a more specific question if necessary. – Joe C Oct 08 '17 at 09:48
  • @Zoe i can't find this "small changes". I ask this question to find an EXACT case. Why can't i do that? – Michael Abyzov Oct 08 '17 at 09:54
  • If you check the answer you see it gets converted to a Date. From the date you can use `getTime()` to get the time in millis from the epoch. That is why it is close enough to be a duplicate – Zoe Oct 08 '17 at 09:57
  • so add the `java.util.TimeZone`'s offset to your `currentTimeMillis` – pskink Oct 08 '17 at 10:07
  • @Zoe `getTime()` are identical `System.currentTimeMillis()`. You are wrong. – Michael Abyzov Oct 08 '17 at 10:37
  • @pskink i don't understand, how can i do that. Give me example pls. – Michael Abyzov Oct 08 '17 at 10:39
  • if your zone is (GMT+3) then add 3600 * 3 * 1000 to the value returned by `currentTimeMillis` – pskink Oct 08 '17 at 10:40
  • @MichaelAbyzov getTime gets the current time in milliseconds in a given timezone. Works with date and hook that up with a Calendar to get the current time and you have the current time in millis without having to do maths as proposed by pskink – Zoe Oct 08 '17 at 10:41
  • just see `java.util.TimeZone` documentation to see how to get the offset – pskink Oct 08 '17 at 10:47
  • Ok guys, but `Calendar.getInstance(TimeZone.getTimeZone("GMT+3")).getTime().getTime()` are equals to `System.currentTimeMillis()`. It doesn't consider my timezone. What i'm doing wrong? – Michael Abyzov Oct 08 '17 at 11:03
  • @pskink can you gimme an example of getting current unix time in my timezone without mathematics? I can't find it in documentation. – Michael Abyzov Oct 08 '17 at 11:11
  • without mathematics? no, you have to add `long` and `int` values – pskink Oct 08 '17 at 11:13
  • @pskink i don't understand you. Is there no way to do it, except adding 3600 * 3 * 1000 ??? I don't believe – Michael Abyzov Oct 08 '17 at 11:17
  • @Zoe gimme a Working example of your solutions. I can't find a way of it. – Michael Abyzov Oct 08 '17 at 11:19
  • just add `currentTimeMillis()` to your current `TimeZone`'s offset - this is one addition of `long` and `int` value - why dont you read `java.util.TimeZone` documentation to see how to get the offset ? – pskink Oct 08 '17 at 11:19
  • 1
    Currenttimemillis is the number of milliseconds since unix epoch (1970-01-01T00:00Z - January 1st 1970 at midnight in UTC). This value is "absolute": right now everywhere in the world are in the same instant (with the same millis value). But this value can correspond to a different date and time in different places. What exactly is the value that you expect? –  Oct 08 '17 at 11:20
  • Yeap, i've read documentation more attentively and found solution. Thanks @pskink and other guys for your feedback! – Michael Abyzov Oct 08 '17 at 12:01
  • @JoeC No, the Joda-Time library is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode). The team advises migrating to the java.time classes. Much of the java.time  functionality is back-ported to Java 6 & Java 7 in the [ThreeTen-Backport](http://www.threeten.org/threetenbp/) project, a project led by the same man who led both Joda-time and java.time, Stephen Colebourne. Further adapted for Android in the [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) project. So no need to recommend Joda-Time any longer. – Basil Bourque Oct 08 '17 at 22:04
  • `org.threeten.bp.ZonedDateTime.now( ZoneId.of( "Europe/Istanbul" ) ).toString()` > `2017-10-09T09:03:35.495+03:00[Europe/Istanbul]` — See [IdeOne.com](https://ideone.com/hOY94c) – Basil Bourque Oct 08 '17 at 22:11

1 Answers1

0

I just needed adding an offset of my timezone. Function below was exactly what i wanted!

public static long getCurrentTimeInUnixFormat(){
    return (System.currentTimeMillis()
            + TimeZone.getTimeZone("GMT+3").getOffset(System.currentTimeMillis())) / 1000;
}
Michael Abyzov
  • 452
  • 1
  • 6
  • 16