添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
public void LocalDate转String() { LocalDate date = LocalDate.now(); DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String dateStr = date.format(fmt); System.out.println("LocalDate转String:"+dateStr);

控制台输出结果

LocalDate转String:2017-11-21

LocalDateTime转String

    @Test
    public void LocalDateTime转String() {        
        LocalDateTime dateTime = LocalDateTime.now();
        //使用预定义实例来转换
        DateTimeFormatter fmt = DateTimeFormatter.ISO_LOCAL_DATE;
        String dateStr = dateTime.format(fmt);
        System.out.println("LocalDateTime转String[预定义]:"+dateStr);
        //使用pattern来转换      
        //12小时制与24小时制输出由hh的大小写决定
        DateTimeFormatter fmt12 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss:SSS");
        String dateStr12 = dateTime.format(fmt12);
        System.out.println("LocalDateTime转String[pattern](12小时制):"+dateStr12);
        DateTimeFormatter fmt24 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");       
        String dateStr24 = dateTime.format(fmt24);      
        System.out.println("LocalDateTime转String[pattern](24小时制):"+dateStr24);
        //如果想要给12小时制时间加上am/pm,这样子做:
        fmt12 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss:SSS a");
        dateStr12 = dateTime.format(fmt12);
        System.out.println("LocalDateTime转String[pattern](12小时制带am/pm):"+dateStr12);

控制台输出结果

LocalDateTime转String[预定义]:2017-11-21
LocalDateTime转String[pattern](12小时制):2017-11-21 02:51:51:550
LocalDateTime转String[pattern](24小时制):2017-11-21 14:51:51:550
LocalDateTime转String[pattern](12小时制带am/pm):2017-11-21 02:51:51:550 下午

String转LocalDate和LocalDateTime

    @Test
    public void String转LocalDate和LocalDateTime(){
        String str = "2017-11-21 14:41:06:612";
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");     
        LocalDate date = LocalDate.parse(str, fmt);
        LocalDateTime time = LocalDateTime.parse(str, fmt);
        System.out.println("date:"+date);
        System.out.println("time:"+time);

控制台输出结果

date:2017-11-21
time:2017-11-21T14:41:06.612

DateTimeFormatter详解

DateTimeFormatter我们更多的是直接使用pattern来做转换,
其实这个类本身已经提供了一些预定义好的实例供我们使用。
下面把两者的具体释义和示例都贴出来供大家参考。

Predefined Formatters                       Formatter Description                                               Example 
----------------------                      ----------------------                                              ------------ 
ofLocalizedDate(dateStyle)                  Formatter with date style from the locale                           '2011-12-03' 
ofLocalizedTime(timeStyle)                  Formatter with time style from the locale                           '10:15:30' 
ofLocalizedDateTime(dateTimeStyle)          Formatter with a style for date and time from the locale            '3 Jun 2008 11:05:30' 
ofLocalizedDateTime(dateStyle,timeStyle)    Formatter with date and time styles from the locale                 '3 Jun 2008 11:05' 
BASIC_ISO_DATE                              Basic ISO date                                                      '20111203' 
ISO_LOCAL_DATE                              ISO Local Date                                                      '2011-12-03' 
ISO_OFFSET_DATE                             ISO Date with offset                                                '2011-12-03+01:00' 
ISO_DATE                                    ISO Date with or without offset                                     '2011-12-03+01:00'; '2011-12-03' 
ISO_LOCAL_TIME                              Time without offset                                                 '10:15:30' 
ISO_OFFSET_TIME                             Time with offset                                                    '10:15:30+01:00' 
ISO_TIME                                    Time with or without offset                                         '10:15:30+01:00'; '10:15:30' 
ISO_LOCAL_DATE_TIME                         ISO Local Date and Time                                             '2011-12-03T10:15:30' 
ISO_OFFSET_DATE_TIME                        Date Time with Offset                                               '2011-12-03T10:15:30+01:00' 
ISO_ZONED_DATE_TIME                         Zoned Date Time                                                     '2011-12-03T10:15:30+01:00[Europe/Paris]' 
ISO_DATE_TIME                               Date and time with ZoneId                                           '2011-12-03T10:15:30+01:00[Europe/Paris]' 
ISO_ORDINAL_DATE                            Year and day of year                                                '2012-337' 
ISO_WEEK_DATE                               Year and Week                                                       '2012-W48-6' 
ISO_INSTANT                                 Date and Time of an Instant                                         '2011-12-03T10:15:30Z'  
RFC_1123_DATE_TIME                          RFC 1123 / RFC 822                                                  'Tue, 3 Jun 2008 11:05:30 GMT' 

Pattern

All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters. The following pattern letters are defined: 
  Symbol  Meaning                     Presentation      Examples
  ------  -------                     ------------      -------
   G       era                         text              AD; Anno Domini; A
   u       year                        year              2004; 04
   y       year-of-era                 year              2004; 04
   D       day-of-year                 number            189
   M/L     month-of-year               number/text       7; 07; Jul; July; J
   d       day-of-month                number            10
   Q/q     quarter-of-year             number/text       3; 03; Q3; 3rd quarter
   Y       week-based-year             year              1996; 96
   w       week-of-week-based-year     number            27
   W       week-of-month               number            4
   E       day-of-week                 text              Tue; Tuesday; T
   e/c     localized day-of-week       number/text       2; 02; Tue; Tuesday; T
   F       week-of-month               number            3
   a       am-pm-of-day                text              PM
   h       clock-hour-of-am-pm (1-12)  number            12
   K       hour-of-am-pm (0-11)        number            0
   k       clock-hour-of-am-pm (1-24)  number            0
   H       hour-of-day (0-23)          number            0
   m       minute-of-hour              number            30
   s       second-of-minute            number            55
   S       fraction-of-second          fraction          978
   A       milli-of-day                number            1234
   n       nano-of-second              number            987654321
   N       nano-of-day                 number            1234000000
   V       time-zone ID                zone-id           America/Los_Angeles; Z; -08:30
   z       time-zone name              zone-name         Pacific Standard Time; PST
   O       localized zone-offset       offset-O          GMT+8; GMT+08:00; UTC-08:00;
   X       zone-offset 'Z' for zero    offset-X          Z; -08; -0830; -08:30; -083015; -08:30:15;
   x       zone-offset                 offset-x          +0000; -08; -0830; -08:30; -083015; -08:30:15;
   Z       zone-offset                 offset-Z          +0000; -0800; -08:00;
   p       pad next                    pad modifier      1
   '       escape for text             delimiter
   ''      single quote                literal           '
   [       optional section start
   ]       optional section end
   #       reserved for future use
   {       reserved for future use
   }       reserved for future use

前面LocalDateTime转String的部分其实已经涉及到了一些DateTimeFormatter的不同使用方法,这里就不再重复提供demo啦。

public static void main(String[] args) { DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime localDateTime = LocalDateTime.now(); String dateStr = localDateTime.fo LocalDate formatDate = LocalDate.of(2020, 2, 5); // 自定义 LocalDate today = LocalDate.now(); // 获取当前日期 1.2、getX() 获取年月日等 注意:获取月份使用getMonthValue()
直接上代码就完事了 DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//定义时间格式 LocalDateTime time = LocalDateTime.now();//拿到现在的时间 String localTime = df.format(time);//现在的时间化为String类型 LocalDateTime ldt = LocalDateTime.parse(localTime, df);//把
要进行datelocaldatelocaldatetimestring之间的相互转换,可以使用Java 8中的DateTimeFormatter和相关类来实现。 1. 将String转换LocalDateTime: 使用DateTimeFormatter的ofPattern方法指定日期时间的格式,然后使用LocalDateTime的parse方法将String转换LocalDateTime对象。例如: ```java String dateStr = "2020-07-20 15:54:41"; String pattern = "yyyy-MM-dd HH:mm:ss"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); LocalDateTime localDateTime = LocalDateTime.parse(dateStr, dateTimeFormatter); 2. 将LocalDateTime转换String: 使用DateTimeFormatter的ofPattern方法指定日期时间的格式,然后使用LocalDateTime的format方法将LocalDateTime对象转换String。例如: ```java LocalDateTime localDateTime = LocalDateTime.now(); String pattern = "yyyy-MM-dd HH:mm:ss"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); String dateStr = localDateTime.format(dateTimeFormatter); 3. 将String转换LocalDate: 使用DateTimeFormatter的ofPattern方法指定日期的格式,然后使用LocalDate的parse方法将String转换LocalDate对象。例如: ```java String dateStr = "2021-06-15"; String pattern = "yyyy-MM-dd"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); LocalDate localDate = LocalDate.parse(dateStr, dateTimeFormatter); 4. 将LocalDate转换String: 使用DateTimeFormatter的ofPattern方法指定日期的格式,然后使用LocalDate的format方法将LocalDate对象转换String。例如: ```java LocalDate localDate = LocalDate.now(); String pattern = "yyyy-MM-dd"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); String dateStr = localDate.format(dateTimeFormatter); 请注意,以上代码示例仅供参考,具体的实现方式可以根据实际需求进行调整。 #### 引用[.reference_title] - *1* [LocalDateLocalDateTime的各种转换形式](https://blog.csdn.net/zzti_erlie/article/details/100657394)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [LocalDate & LocalDateTime互相转换](https://blog.csdn.net/weixin_45103793/article/details/117929986)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]