要获取当前字符串时间的格式为"yyyy-mm-dd hh:mm:ss",可以使用Java的SimpleDateFormat类来实现。

首先,需要导入java.text.SimpleDateFormat类,然后创建SimpleDateFormat对象,并指定要格式化的日期时间模式。在这个例子中,模式为"yyyy-MM-dd HH:mm:ss"。

接下来,通过调用SimpleDateFormat对象的format()方法,传入当前日期时间对象new Date(),即可得到当前字符串时间。

下面是一个完整的示例代码:

import java.text.SimpleDateFormat; import java.util.Date;

public class GetCurrentTimeString { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTimeString = sdf.format(new Date()); System.out.println(currentTimeString); } }

运行这段代码,将会输出当前时间的字符串形式,格式为"yyyy-mm-dd hh:mm:ss"。

java 获取当前字符串时间yyyy-mm-dd hhmmss

原文地址: https://www.cveoy.top/t/topic/i5zV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录