将日期转换为天
•浏览 1
Convert date into days
本问题已经有最佳答案,请猛点这里访问。
如何将以下格式的日期转换为分钟、小时、天、周。一般使用这个信息来显示消息发布信息。
2020-04-11T17:41:00Z
你的问题不是很清楚。所以,我在这里写一些示例代码供您理解。正如您在此示例中所见,您将进一步探索 java.time。和 java.time.format。与日期和时间相关的类非常丰富。避免使用过时的 java.util.* 日期和时间 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());
}
}
Year: 2020
Month: 4
Day: 11
Hour: 17
Minute: 41
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddThh:mm:ssZ");
Date dt = sdf.parse(string);
sdf.format(YourDateObject)
public boolean isAfterPayDay(DateTime datetime) {
if (datetime.getMonthOfYear() == 2) { // February is month 2!!
return datetime.getDayOfMonth() > 26;
}
return datetime.getDayOfMonth() > 28;
}
输出:
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());
}
}
Year: 2020
Month: 4
Day: 11
Hour: 17
Minute: 41
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddThh:mm:ssZ");
Date dt = sdf.parse(string);
sdf.format(YourDateObject)
public boolean isAfterPayDay(DateTime datetime) {
if (datetime.getMonthOfYear() == 2) { // February is month 2!!
return datetime.getDayOfMonth() > 26;
}
return datetime.getDayOfMonth() > 28;
}
不太清楚你的意思,所以我会在这个前提下工作。输入此格式的字符串:2020-04-11T17:41:00Z。至于将其转换为分钟、小时等的意思。我假设将其转换为日期对象?如果我错了请纠正我
小提示:如果您是日期格式的控制者,在转换为该字符串之前,我建议您使用 Unix 时间(A.K.A. POSIX 时间 - 在线阅读)。
如果我的解释是正确的,你可以使用 SimpleDateFormat
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());
}
}
Year: 2020
Month: 4
Day: 11
Hour: 17
Minute: 41
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddThh:mm:ssZ");
Date dt = sdf.parse(string);
sdf.format(YourDateObject)
public boolean isAfterPayDay(DateTime datetime) {
if (datetime.getMonthOfYear() == 2) { // February is month 2!!
return datetime.getDayOfMonth() > 26;
}
return datetime.getDayOfMonth() > 28;
}
您还可以使用 sdf 将您的日期对象转换为您的给定格式:
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());
}
}
Year: 2020
Month: 4
Day: 11
Hour: 17
Minute: 41
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddThh:mm:ssZ");
Date dt = sdf.parse(string);
sdf.format(YourDateObject)
public boolean isAfterPayDay(DateTime datetime) {
if (datetime.getMonthOfYear() == 2) { // February is month 2!!
return datetime.getDayOfMonth() > 26;
}
return datetime.getDayOfMonth() > 28;
}
考虑使用 Joda Time 库,它很受欢迎,很多人都在使用它,因为它很简单。
它有你需要的自定义类型。乔达网站
他们网站上的示例代码:
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());
}
}
Year: 2020
Month: 4
Day: 11
Hour: 17
Minute: 41
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddThh:mm:ssZ");
Date dt = sdf.parse(string);
sdf.format(YourDateObject)
public boolean isAfterPayDay(DateTime datetime) {
if (datetime.getMonthOfYear() == 2) { // February is month 2!!
return datetime.getDayOfMonth() > 26;
}
return datetime.getDayOfMonth() > 28;
}