正在显示
1 个修改的文件
包含
42 行增加
和
6 行删除
@@ -3,12 +3,14 @@ package com.tianbo.util.Date; | @@ -3,12 +3,14 @@ package com.tianbo.util.Date; | ||
3 | import javafx.util.converter.LocalDateTimeStringConverter; | 3 | import javafx.util.converter.LocalDateTimeStringConverter; |
4 | 4 | ||
5 | import java.text.ParseException; | 5 | import java.text.ParseException; |
6 | +import java.text.ParsePosition; | ||
6 | import java.text.SimpleDateFormat; | 7 | import java.text.SimpleDateFormat; |
7 | import java.time.*; | 8 | import java.time.*; |
8 | import java.time.format.DateTimeFormatter; | 9 | import java.time.format.DateTimeFormatter; |
9 | import java.time.format.DateTimeParseException; | 10 | import java.time.format.DateTimeParseException; |
10 | import java.util.Calendar; | 11 | import java.util.Calendar; |
11 | import java.util.Date; | 12 | import java.util.Date; |
13 | +import java.util.TimeZone; | ||
12 | 14 | ||
13 | 15 | ||
14 | /** | 16 | /** |
@@ -102,13 +104,16 @@ public final class DateUtil { | @@ -102,13 +104,16 @@ public final class DateUtil { | ||
102 | * @throws DateTimeParseException | 104 | * @throws DateTimeParseException |
103 | */ | 105 | */ |
104 | public static Date formatByyyyyMMddHHmmss(String dateStr) throws DateTimeParseException{ | 106 | public static Date formatByyyyyMMddHHmmss(String dateStr) throws DateTimeParseException{ |
105 | - //毫秒级的去掉 | ||
106 | - if(dateStr.length()>14){ | ||
107 | - dateStr= dateStr.substring(0,14); | 107 | + if (dateStr.length()>0){ |
108 | + //毫秒级的去掉 | ||
109 | + if(dateStr.length()>14){ | ||
110 | + dateStr= dateStr.substring(0,14); | ||
111 | + } | ||
112 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); | ||
113 | + LocalDateTime dateTime = LocalDateTime.parse(dateStr, formatter); | ||
114 | + return convertLDTToDate(dateTime); | ||
108 | } | 115 | } |
109 | - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); | ||
110 | - LocalDateTime dateTime = LocalDateTime.parse(dateStr, formatter); | ||
111 | - return convertLDTToDate(dateTime); | 116 | + return null; |
112 | } | 117 | } |
113 | 118 | ||
114 | //LocalDateTime转换为Date | 119 | //LocalDateTime转换为Date |
@@ -540,4 +545,35 @@ public final class DateUtil { | @@ -540,4 +545,35 @@ public final class DateUtil { | ||
540 | 545 | ||
541 | return time; | 546 | return time; |
542 | } | 547 | } |
548 | + | ||
549 | + /** | ||
550 | + * | ||
551 | + * @doc 日期转换星期几 | ||
552 | + * @param datetime | ||
553 | + * 日期 例:2017-10-17 | ||
554 | + * @return String 例:星期二 | ||
555 | + * @author lzy | ||
556 | + * @history 2017年10月17日 上午9:55:30 Create by 【lzy】 | ||
557 | + */ | ||
558 | + public static String dateToWeek(String datetime) throws java.text.ParseException { | ||
559 | + SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd"); | ||
560 | + f.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); | ||
561 | + f.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); | ||
562 | + String[] weekDays = { "7","1" , "2", "3", "4", "5", "6"}; | ||
563 | + Calendar cal = Calendar.getInstance(); // 获得一个日历 | ||
564 | + Date datet = null; | ||
565 | + datet = (Date) f.parse(datetime); | ||
566 | + cal.setTime(datet); | ||
567 | + int w = cal.get(Calendar.DAY_OF_WEEK)-1; // 指示一个星期中的某天。 | ||
568 | + if (w < 0) | ||
569 | + w = 0; | ||
570 | + return weekDays[w]; | ||
571 | + } | ||
572 | + | ||
573 | + public static void main(String[] args) throws ParseException { | ||
574 | + String s = dateToWeek("20200815"); | ||
575 | + System.out.println(s); | ||
576 | + } | ||
577 | + | ||
578 | + | ||
543 | } | 579 | } |
-
请 注册 或 登录 后发表评论