DateTimeFormatter

来源:互联网 发布:多玩魔兽世界数据库 编辑:程序博客网 时间:2024/06/06 23:17
package day2;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;import java.time.format.FormatStyle;import java.util.Calendar;/* 2017-02-16 17:14:55 * */public class DateTimeFormatterj {public static void main(String[] args) {//DateTimeFormatter 不仅可以将日期、时间对象格式化成字符串,也可以将特定的格式的字符串解析成日期、时间对象/* * 获取DateTimeFormatter 对象的三种格式 * 直接使用静态常量创建  DateTimeFormatter 格式器 。 *  * FormatStyle 枚举类中定义了 FULL LONG MEDIUM SHORT 四个枚举值 *  * 根据模式字符串来创建DateTimeFormatter  ,可以采用模式字符串来创建 * *///formattej();/* * DateTimeFormatter  解析字符串 * */String string = "2017-02-16 18:27:09";DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");LocalDateTime localDateTime = LocalDateTime.parse(string,dateTimeFormatter);System.out.println(localDateTime);}/* 2017-02-16 18:25:50 * 格式化 */private static void formattej() {DateTimeFormatter[] formatters = new DateTimeFormatter[] { //使用常量创建DateTimeFormatter.ISO_LOCAL_DATE,DateTimeFormatter.ISO_LOCAL_TIME,DateTimeFormatter.ISO_LOCAL_DATE_TIME,//使用本地化的不同风格来创建DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL,FormatStyle.MEDIUM),//根据模式来创建DateTimeFormatter.ofPattern("GyyyyMMdd HH:mm:ss")};LocalDateTime dateTime = LocalDateTime.now();for (int i = 0; i < formatters.length; i++) {//System.out.println(dateTime.format(formatters[i])); //System.out.println(formatters[i].format(dateTime));//两句相同}for (DateTimeFormatter dateTimeFormatter : formatters) {System.out.println(dateTime.format(dateTimeFormatter));}int[] arr = new  int[] {1,2,3};for (int j : arr) {System.out.println(j);}Calendar calendar =Calendar.getInstance();System.out.println(calendar.getTime());}}

0 0
原创粉丝点击