-2

I've seen many useful responses on this, but using classes unavailable to me.

I have a correct Date object, and i've tryied different things, but when a review the result here I can see, it's not working.

I can only use Java7 (or kotlin equivalent)

Here is my current incorrect code:

@JvmStatic fun getEpochNumberFromDate(date: Date):String{

        var calendar : Calendar = Calendar.getInstance()
        calendar.isLenient = false
        calendar.set(date.year, date.day,date.month,date.hours,date.minutes)

        Log.d("zzz","calendar date : "+calendar.timeInMillis.toString())

        Log.d("zzz","year: "+date.year+", month:"+date.month+",  date.time.toString():"+date.time.toString())


        return date.time.toString()
    }
LearningCharlito
  • 327
  • 1
  • 2
  • 20
  • Can you elaborate your problem in a more specific way ? – Sambit May 13 '19 at 20:24
  • 3
    "I have a correct Date object" - I suspect you don't actually. Currently you're using the system default time zone - is that deliberate? It's worth noting that `Date.getYear()` etc have been deprecated for *decades* and I'd strongly encourage you not to use them. – Jon Skeet May 13 '19 at 20:26
  • 1
    Could you provide an example input (Date) and output? The fact that the method is named getEpochNumberFromDate but returns a string is quite confusing. The fact that the 5 first lines of the method don't do anything useful is confusing too. What are you trying to achieve? – JB Nizet May 13 '19 at 20:28
  • If you have a (deprecated, old, etc.) `java.util.Date` object, is there something wrong with the `.getTime()` method? – Matt Johnson-Pint May 13 '19 at 20:29
  • 1
    FYI, the troublesome date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Most of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android (<26) in [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP). See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque May 14 '19 at 00:07
  • Always search Stack Overflow before posting. Your issue almost certainly addressed. And if it weren't, searching would have educated about the various issues so you could have more clearly stated a specific question with example code. – Basil Bourque May 14 '19 at 00:07
  • I believe that the code you have is correct. On Android API level 21 do consider ThreeTenABP instead of the poorly designed `Date` and `Calendar`. – Ole V.V. May 14 '19 at 07:13

1 Answers1

1

You're overcomplicating it. There is no need to use a Calendar at all. Date.getTime() returns milliseconds since epoch. You just need to divide by 1000. If you're seeing other differences from that website, it's probably because of timezone issues.

Laplie Anderson
  • 6,345
  • 4
  • 33
  • 37
  • Since an old-fashioned `Date` doesn’t have a time zone (and the epoch is time zone independent too), if you use `Date` correctly, there shouldn’t be any time zone issues. – Ole V.V. May 14 '19 at 06:54