La conversion entre la chaîne de date

Avant JDK8, la conversion mutuelle entre plusieurs chaînes implémentée en utilisant java.text.SimpleDateFormat

Le temps de chaîne

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(format.format(new Date()));

16/03/2020 23:57:36

temps de transfert de chaîne

SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
System.out.println(format.parse("2010年4月23日 9时34分12秒"));

Ven 23 avril 09:34:12 CST 2010

Après JDK8, souvent utilisé java.time.format.DateTimeFormatter (match exact pour le format haute)

Le temps de chaîne

DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(LocalDateTime.now().format(pattern));

17/03/2020 00:10:00

DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(LocalDate.now().format(pattern));

17/03/2020

DateTimeFormatter pattern = DateTimeFormatter.ofPattern("HH:mm:ss");
System.out.println(LocalTime.now().format(pattern));

00:12:04
Note: Le temps correspondent format doit avec l'autre, ou bien il y aura erreur
LocalDateTime.now () -> date, minutes et secondes
LocalDate.now () -> Date
LocalTime.now () -> minutes et secondes

temps de transfert de chaîne

DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
System.out.println(LocalDateTime.parse("2010年04月23日 09时34分12秒", pattern));

2010-04-23T09: 34: 12

DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
System.out.println(LocalDate.parse("2010年04月23日", pattern));

2010-04-23

ateTimeFormatter pattern = DateTimeFormatter.ofPattern("HH时mm分ss秒");
System.out.println(LocalTime.parse("09时34分12秒", pattern));

09:34:12

Publié 27 articles originaux · louange gagné 1 · vues 845

Je suppose que tu aimes

Origine blog.csdn.net/weixin_44971379/article/details/104911819
conseillé
Classement