使用Java中的SimpleDateFormat类可以将Calendar格式化为String。

例如,将当前日期和时间格式化为字符串:

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sdf.format(cal.getTime());
System.out.println(dateStr);

输出:

2021-10-05 14:30:00

在这个例子中,我们获取了当前的Calendar实例,并使用SimpleDateFormat类将其格式化为指定的日期和时间格式,然后将其转换为字符串。

Calendar格式化成string

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

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