-2

How to convert below format date into minutes, hrs, days, weeks. Generally using this info to display message posted information.

2020-04-11T17:41:00Z

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
  • 1
    What do you mean in minutes and etc.? You want to fetch part of date or date in particular unit? – StoneCountry Apr 11 '20 at 21:02
  • There are many posts here that answer your question (https://stackoverflow.com/questions/4216745/java-string-to-date-conversion/). – Mostav Apr 11 '20 at 21:04
  • i want to display like this.........If the date is 15 min ago to current time...i have to display "15min ago"...if the date 1 day ago...i have to display "1 day ago". –  Apr 11 '20 at 21:11
  • `DateTimeFormatter.ISO_DATE_TIME.parse(data).get(ChronoField.DAY_OF_MONTH)` – Johannes Kuhn Apr 11 '20 at 21:13
  • Welcome to SO community, what are the T and 00Z ? – Zain Apr 11 '20 at 21:21
  • @Zain The format is [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601), which defines `T` and `Z` and everything in the string. – Ole V.V. Apr 12 '20 at 22:06
  • @OleV.V. Thanks bro :) – Zain Apr 12 '20 at 22:12

3 Answers3

1

Your question is not so clear. So, I'm writing here some sample code for your understanding. As you can see in this example and you will further explore, java.time. and java.time.format. are quite rich with classes related to date and time. Avoid using the outdated java.util.* date and time API.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        String str = "2020-04-11T17:41:00";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
        System.out.println("Year: " + dateTime.getYear());
        System.out.println("Month: " + dateTime.getMonthValue());
        System.out.println("Day: " + dateTime.getDayOfMonth());
        System.out.println("Hour: " + dateTime.getHour());
        System.out.println("Minute: " + dateTime.getMinute());
    }
}

Output:

Year: 2020
Month: 4
Day: 11
Hour: 17
Minute: 41
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

Not quite sure what you mean, so I will work with this premise. A string in this format is inputted: 2020-04-11T17:41:00Z. As for what you mean to convert it to minutes, hours, etc. I am assuming to make it to a date object? Please correct me if I am wrong

Small tip: If you are the one in control of the date format, before it is turned to this string, I would recommend you use Unix time (A.K.A. POSIX time - read about it online).

If my interpretation is correct, you can use SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddThh:mm:ssZ");
Date dt = sdf.parse(string);

You can also use sdf to convert your date object into your given format with:

sdf.format(YourDateObject)
omar jayed
  • 850
  • 5
  • 16
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Also your code throws `java.lang.IllegalArgumentException: Illegal pattern character 'T'`. – Ole V.V. Apr 12 '20 at 22:08
-1

Consider using the Joda Time library, it's a popular one and many people are using it because of it's simplicity.

It has custom types for what you need. Joda Website

Example code from their website:


public boolean isAfterPayDay(DateTime datetime) {
  if (datetime.getMonthOfYear() == 2) {   // February is month 2!!
    return datetime.getDayOfMonth() > 26;
  }
  return datetime.getDayOfMonth() > 28;
}
motoquick
  • 108
  • 1
  • 7
  • 1
    Since most of the functionality of Joda Time was added into Java 8, the Joda Time web site recommends migtating to that. (As I understand it, It's likely to be much more standard, guaranteed to be available without adding any dependencies, and so a better option for most purposes.) – gidds Apr 11 '20 at 22:44