0

I have an app on iOS and am working on porting over to android. I am having an issue with handling the time.

On iOS I store points of time using:

Int(NSDate().timeIntervalSince1970-0.5)

and Convert it to HH:MM:SS using:

private let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
return dateFormatter.string(from: Date(timeIntervalSince1970: value))

On Android(Kotlin) to store the point of time I use: System.currentTimeMillis()/1000

And for conversion:

LocalTime.MIN.plus(Duration.ofSeconds(xVal)).toString())

The issue is that the value after being converted is 6 hours ahead(could be more or less but I only look at the HH:MM:SS). As far as I can tell, the way that I get the time on android is correct and I think the issue lies in when I am converting it. I have looked around for more methods of converting time since an an interval to time of day but I could not get it to work. Most posts are just converting seconds to HH:MM:SS and so those are right out of the question. What would the equivalent to my iOS code be?

EDIT I've also tried using this method to convert it still is 6 hours off even though I am giving it a timezone:

    val cal = Calendar.getInstance()
    cal.timeInMillis = xVal * 1000L
    cal.timeZone = TimeZone.getTimeZone(TimeZone.getDefault().displayName)
    Log.i("This is the cal value",cal.get(Calendar.HOUR_OF_DAY).toString())
Mumphus
  • 303
  • 4
  • 20
  • Are you in the UTC timezone? if not you need to set the timezone when you convert or else it will be off – tyczj Jul 30 '18 at 18:02
  • question is already answered .. check https://stackoverflow.com/questions/11357945/java-convert-seconds-into-day-hour-minute-and-seconds-using-timeunit – Muhammad Hassaan Jul 30 '18 at 18:06
  • @MuhammadHassaan as I stated in the question that is not converting time from Unix Epoch – Mumphus Jul 30 '18 at 18:08
  • @tyczj How would I go about getting the current time zone and apply to the value? – Mumphus Jul 30 '18 at 18:09
  • A simple google search can answer how to get the time zone, here is a basic example https://stackoverflow.com/questions/6088778/converting-utc-dates-to-other-timezones – tyczj Jul 30 '18 at 18:17

1 Answers1

0
val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
val date = sdf.format(Date(alarm.getNextTrigger()));
shiftpsh
  • 1,926
  • 17
  • 24