---
title: "Java 8 获取当前日期"
author: "fairy_xzc"
date: "2018-12-12T14:38:14Z"
tags: []
categories: ["编程语言"]
slug: "java-8"
draft: false
lastmod: "2018-12-12T14:38:59Z"
id: 2435106
url: "https://agilestyle.iteye.com/blog/2435106"
---

**原创转载请注明出处：[https://agilestyle.iteye.com/blog/2435106](/blog/2435106)**

 

Code Demo

```
public static String getCurrentDateTime() {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    return LocalDateTime.now().format(formatter);
}

public static String getCurrentDateTime(String format) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
    return LocalDateTime.now().format(formatter);
}
```

 

Test

```
System.out.println(getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
System.out.println(getCurrentDateTime("yyMMddHHmmss"));
System.out.println(getCurrentDateTime("yyyyMMdd"));
System.out.println(getCurrentDateTime("HHmmss"));
System.out.println(getCurrentDateTime());
```

 

Console Output

![](http://dl2.iteye.com/upload/attachment/0131/1708/22b0faf9-f1dc-3511-82c8-a106ad6168f2.png)