DateTimeFormatter
2017-02-16 21:20
288 查看
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()); } }
相关文章推荐
- java 8 LocalDateTime,DateTimeFormatter
- Poco::DateTimeFormatter Tips
- 利用DateTimeFormatter 来格式化时间时,报DateTimeParseException异常
- Instant、LocalDateTime与DateTimeFormatter
- Java8新增的DateTimeFormatter与SimpleDateFormat的区别
- LocalDateTime and DAteTimeFormatter
- LocalDate LocalTime LocalDateTime DateTimeFormatter Java 8 新出的时间(time)和日期(date)的API的使用详细实例
- SimpleDateFormat 线程安全的解决方案--DateTimeFormatter
- Java的日期与时间(十六)java.time.format.DateTimeFormatter
- 日期格式器(NSDateFormatter)
- String Format for DateTime
- NSDateFormatter 基本用法
- NSDateFormatter转换时间字符串时的时区问题
- An exception has occurred while using the formatter ‘JsonMediaTypeFormatter’ to generate sample
- NSDateFormatter和NSNumberFormatter以及格式化限制
- NSDate, NSDateFormatter的使用
- Java CodeFormatter
- 用NSDateFormatter调整时间格式
- NSDateFormatter替代
- [apple]NSFormatter for IP