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
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
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
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)
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;
}