-7

I am developing news app and I am getting this kind from server 2019-10-01T02:49:00Z

I want to convert that time to minutes like below photo (elapsed time from now to that date) and pass that method to viewholder.

image

below my viewholder class class TopHeadlinesAdapter(val context: Context) : RecyclerView.Adapter() {

var articleList : List<Article> = listOf()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

    val view = LayoutInflater.from(parent.context).inflate(R.layout.news_list,parent,false)
    return MyViewHolder(view)
}

override fun getItemCount(): Int {
    return articleList.size
}

@SuppressLint("NewApi")
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

    holder.articleTitle.text = articleList.get(position).title
    holder.articleSourceName.text = articleList.get(position).source.name
    Picasso.get().load(articleList.get(position).urlToImage).into(holder.image)
    holder.articleTime.text = articleList.get(position).publishedAt



}

fun setMovieListItems(articleList: List<Article>){
    this.articleList = articleList
    notifyDataSetChanged()
}

@SuppressLint("NewApi")
fun example( time:String) {

    val timelinePoint = LocalDateTime.parse("2019-10-01T02:49:00")
    val now = LocalDateTime.now()

    var elapsedTime = Duration.between(timelinePoint, now)

    println(timelinePoint)
    println(now)

    println(elapsedTime.toMinutes())



}
class MyViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {

    val image: ImageView = itemView!!.findViewById(R.id.imageView)
    val articleTitle: TextView = itemView!!.findViewById(R.id.articleTitle)
    val articleSourceName: TextView = itemView!!.findViewById(R.id.articleSourceName)
    val imageCategory: ImageView = itemView!!.findViewById(R.id.imageCategory)
    val articleTime: TextView = itemView!!.findViewById(R.id.articleTime)

}

}

Edgar Shvedskiy
  • 135
  • 3
  • 18
  • 3
    Hi Edgar, your question is getting down-voted because your question does not provide enough information to understand what you're trying to do. What is the output expect for the server timestamp in your question? Please read [**how to ask**](https://stackoverflow.com/help/how-to-ask) and [**how to create a minimum, reproducible example**](https://stackoverflow.com/help/minimal-reproducible-example) for better results when using the site. Good luck! – kenny_k Oct 08 '19 at 11:52
  • hi kenny_k I am getting string date time 2019-10-01T02:49:00Z I want to convert to minutes – Edgar Shvedskiy Oct 08 '19 at 11:53
  • 2
    Hi Edgar, you need to ready kenny_k's comment again and carefully. What he is telling you is that its difficult to answer your question, not because he doesn't know the solution, but because your question could mean anything. What is is exactly you are asking? The headline reads "hours to minutes" and in the question you say you have a date time. Ok. But what exactly do you want to convert to minutes? Whats the exact input ,what computation do you need and what output do you expect? See, your question itself raises many questions, thats why people don't answer. An example would help a lot. – Benjamin Basmaci Oct 08 '19 at 12:12
  • I have put screenshot what kind of output I want first check my question then comment – Edgar Shvedskiy Oct 08 '19 at 12:13
  • See, you added the picture but again, its difficult to know what you want exactly. You see, taking the date time you provided, namely "2019-10-01T02:49:00Z", I can't see how the "17 min" of the picture come to be. The date time shows 49 minutes and 2 hours. That would make 169 minutes. But is that what you want? – Benjamin Basmaci Oct 08 '19 at 12:14
  • yes I want to show minutes like on the picture your approach correct – Edgar Shvedskiy Oct 08 '19 at 12:19
  • You still haven't clarified what you want. A time isn't minutes. Do you want the number of minutes from now until that time? Or how many minutes past midnight is that time? Or how many minutes since the epoch is that time? etc. – Tenfour04 Oct 08 '19 at 13:01
  • It's quite obvious that OP wants to see elapsed time, from now to date he get from server (it's how news like app works). – Cililing Oct 08 '19 at 13:02
  • gotcha--------- – Tenfour04 Oct 08 '19 at 13:04
  • 2
    Possible duplicate of [How to convert time to “ time ago ” in android](https://stackoverflow.com/questions/35858608/how-to-convert-time-to-time-ago-in-android) and/or other questions – Ole V.V. Oct 08 '19 at 13:33
  • @Cililing now it's obvious, but firstly, there was no picture so it wasn't that obvious – dey Oct 08 '19 at 13:46

1 Answers1

3

Time to minutes? What does it mean?

I guess you want to display elapsed time from now to news' publish date. If so, you can use Java Time API from Java8:

import java.time.Duration
import java.time.LocalDateTime

fun example() {
    val timelinePoint = LocalDateTime.parse("2019-10-01T02:49:00")
    val now = LocalDateTime.now()

    val elapsedTime = Duration.between(timelinePoint, now)

    println(timelinePoint)
    println(now)

    println(elapsedTime.toMinutes())
}

Cililing
  • 4,303
  • 1
  • 17
  • 35