在Java开发中,处理日期时间数据时经常会遇到格式化问题,其中最常见的就是时间部分末尾出现“.0”的情况,将LocalDateTime或Date对象转换为字符串时,可能会得到类似“2023-10-15 14:30:00.0”这样的结果,这种“.0”通常是由于时间精度保留到毫秒位,而毫秒值恰好为0时系统自动补全显示导致的,虽然不影响数据准确性,但在日志输出、数据展示或接口返回时,这种格式往往不够美观,甚至可能引起用户困惑,本文将详细介绍几种在Java中有效去除时间部分末尾“.0”的方法,帮助开发者根据实际场景选择最合适的解决方案。

使用SimpleDateFormat进行格式化
SimpleDateFormat是Java中经典的日期时间格式化类,通过自定义模式字符串可以精确控制输出格式,要去除“.0”,只需在模式中不包含毫秒部分的标识符即可。
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(date);
System.out.println(formattedDate); // 输出格式如:2023-10-15 14:30:00
}
}
关键点:模式字符串"yyyy-MM-dd HH:mm:ss"中未包含SSS(毫秒),因此不会输出毫秒部分,如果输入数据的毫秒值非0,此方法会直接截断,而非四舍五入,需要注意的是,SimpleDateFormat是线程不安全的,在多线程环境下应使用ThreadLocal或每次新建实例。
利用DateTimeFormatter(Java 8+)
Java 8引入了java.time包,提供了更现代、更安全的日期时间API。DateTimeFormatter是其中的格式化工具,支持LocalDateTime、ZonedDateTime等类,使用方法如下:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatExample {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = now.format(dtf);
System.out.println(formattedDate); // 输出格式如:2023-10-15 14:30:00
}
优势:DateTimeFormatter是线程安全的,且API设计更符合面向对象思想,模式规则与SimpleDateFormat基本一致,但支持更多高级功能,如自定义解析、时区处理等,对于LocalDateTime对象,默认已包含纳秒精度,但通过格式化可以灵活控制输出粒度。
处理数据库返回的时间戳
从数据库(如MySQL、Oracle)查询时间字段时,JDBC驱动可能会将时间戳解析为带毫秒的Timestamp对象,直接调用toString()方法会输出“.0”,解决方案有两种:

-
使用
PreparedStatement设置类型:
在查询时通过setTimestamp方法明确指定类型,避免自动补全毫秒:Timestamp timestamp = resultSet.getTimestamp("create_time"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(timestamp)); -
数据库层面格式化:
在SQL查询中使用函数(如MySQL的DATE_FORMAT)直接格式化时间:SELECT DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') FROM table_name;
处理JSON序列化中的时间格式
在Spring Boot等框架中,返回JSON数据时,日期时间字段可能被序列化为带“.0”的字符串,可通过配置Jackson或Gson的日期格式化规则解决:
Jackson配置(Spring Boot):
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
return mapper;
}
}
Gson配置:

Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
处理字符串替换(不推荐)
对于已包含“.0”的字符串,可通过正则表达式直接替换:
String dateStr = "2023-10-15 14:30:00.0";
String result = dateStr.replaceAll("\\.0$", "");
System.out.println(result); // 输出:2023-10-15 14:30:00
缺点:此方法属于“事后补救”,无法处理毫秒非0的情况(如“.123”),且可能误删其他位置的“.0”,仅适用于数据格式固定且无法修改源头的场景。
最佳实践建议
- 优先使用
java.timeAPI:对于新项目,推荐使用LocalDateTime、ZonedDateTime等类配合DateTimeFormatter,避免线程安全问题。 - 统一格式化规则:在项目中定义日期时间格式常量,确保所有模块输出一致。
- 数据库交互优化:在数据库查询时直接格式化时间,减少应用层处理开销。
- 避免字符串操作:除非万不得已,不要依赖字符串替换处理日期时间格式,优先从数据源头控制输出。
通过以上方法,开发者可以灵活应对Java中时间格式“.0”的问题,确保数据展示的规范性和可读性,选择具体方案时,需结合项目使用的Java版本、框架特性及性能需求综合考虑。
















