获取当前时间 yyyy-bb-mm
时间: 2023-08-07 13:12:50 浏览: 42
要获取当前时间的年份、月份和日期,并以"yyyy-mm-dd"的格式显示,可以使用`java.time.LocalDate`类和`java.time.format.DateTimeFormatter`类。
以下是示例代码:
```java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 定义日期格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 格式化日期
String formattedDate = currentDate.format(formatter);
System.out.println("当前日期:" + formattedDate);
}
}
```
这样就可以获得当前日期的字符串表示,格式为"yyyy-mm-dd"。
希望这对你有帮助!如果还有其他问题,请随时问我。
相关问题
java获取当前时间 yyyy-mm-dd
Java中获取当前时间的方法有很多种,其中一种是使用SimpleDateFormat类将当前时间格式化为指定格式的字符串。具体实现代码如下所示:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String currentDate = sdf.format(new Date());
System.out.println(currentDate);
}
}
```
运行以上代码,可以得到当前日期的字符串形式,格式为yyyy-MM-dd。
js获取当前时间yyyy-mm-dd
可以使用JavaScript的Date对象来获取当前时间,然后使用一些字符串操作方法来格式化日期。下面是一个示例代码:
```javascript
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth() + 1;
var day = today.getDate();
// 添加前导零
if (month < 10) {
month = '0' + month;
}
if (day < 10) {
day = '0' + day;
}
var currentDate = year + '-' + month + '-' + day;
console.log(currentDate);
```
这个代码会输出当前的日期,格式为yyyy-mm-dd。