可以使用SimpleDateFormat类来格式化日期时间,并使用“HH:mm:ss”格式来打印不带毫秒的时间。以下是一个示例代码: ```java import java.text.SimpleDateFormat; import java.util.Date; public class DateTimeExample { public static void main(String[] args) { Date now = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = formatter.format(now); System.out.println("Current date and time: " + formattedDate); formatter = new SimpleDateFormat("HH:mm:ss"); formattedDate = formatter.format(now); System.out.println("Current time without milliseconds: " + formattedDate); } } ``` 输出: ``` Current date and time: 2021-10-01 14:30:45 Current time without milliseconds: 14:30:45 ```