How convert timestamp in Kotlin

Viewed 7147

l am try to convert timeestamp coming from data json url

TimeFlight.text = list[position].TimeFlight.getDateTime(toString())

l am use list view in my app

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

    val view : View = LayoutInflater.from(context).inflate(R.layout.row_layout,parent,false)


    val TimeFlight = view.findViewById(R.id.time_id) as AppCompatTextView
    val LogoAriline = view.findViewById(R.id.logo_image) as ImageView

    status.text= list[position].Stauts
    TimeFlight.text = list[position].TimeFlight.getDateTime(toString())
    Picasso.get().load(Uri.parse("https://www.xxxxxxxxx.com/static/images/data/operators/"+status.text.toString()+"_logo0.png"))
        .into(LogoAriline)



    return view as View
}

private fun getDateTime(s: String): String? {
    try {
        val sdf = SimpleDateFormat("MM/dd/yyyy")
        val netDate = Date(Long.parseLong(s))
        return sdf.format(netDate)
    } catch (e: Exception) {
        return e.toString()
    }
}

Data class for json

data class FlightShdu (val Airline : String ,val TimeFlight : String)

l used that code getDateTime but the format unknown

see result image

3 Answers

Assuming TimeFlight is a stringified epoch timestamp (in milliseconds), you should pass that to your getDateTime() function:

TimeFlight.text = getDateTime(list[position].TimeFlight)

(if they are not millis but seconds, then simply multiply them by 1000 before passing them to the Date constructor)

On a side note, depending on the exact use case, creating a new SimpleDateFormat object might not be necessary on every getDateTime() call, you can make it an instance variable.

Also, i'd advise you to take a look at (and follow) the Java naming conventions for both Java and Kotlin applications.

The problem here is that the Date constructor take long as the milliseconds count since 1/1/1970 and the number you are getting is the seconds count.

my suggestion is the following code( you can change the formate):

const val DayInMilliSec = 86400000

private fun getDateTime(s: String): String? {
    return try {
        val sdf = SimpleDateFormat("MM/dd/yyyy")
        val netDate = Date(s.toLong() * 1000 ).addDays(1)
        sdf.format(netDate)
    } catch (e: Exception) {
        e.toString()
    }
}

fun Date.addDays(numberOfDaysToAdd: Int): Date{
    return Date(this.time + numberOfDaysToAdd * DayInMilliSec)
}
private fun getDateTime(s: String): String? {
    return try {
        val date = SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(Date(s.toLong()*1000))

        // current timestamp in sec
        val epoch = System.currentTimeMillis()/1000
        // Difference between two epoc
        val dif = epoch - s.toLong()
        val timeDif: String
        when {
            dif<60 -> {
                timeDif = "$dif sec ago"
            }
            dif/60 < 60 -> {
                timeDif = "${dif/60} min ago"
            }
            dif/3600 < 24 -> {
                timeDif = "${dif/3600} hour ago"
            }
            dif/86400 < 360 -> {
                timeDif = "${dif/86400} day ago"
            }
            else ->{
                timeDif = "${dif/31556926} year ago"
            }
        }
        "($timeDif) $date"

    } catch (e: Exception) {
        e.toString()
    }
}
Related