Java中Date类的输出格式

本文详细介绍了Java中使用内置日期格式化过程的方法,通过实例展示了如何获取短、中、长和完整日期格式。
使用标准的日期格式化过程 
既然我们已经可以生成和解析定制的日期格式了, 让我们来看一看如何使用内建的格式化过程. 方法 DateFormat.getDateTimeInstance() 让我们得以用几种不同的方法获得标准的日期格式化过程. 在下面的例子中, 我们获取了四个内建的日期格式化过程. 它们包括一个短的, 中等的, 长的, 和完整的日期格式. 
import java.text.DateFormat; 
import java.util.Date; 
public  class DateExample4  {     
public static void main(String[] args) 

{  
   Date date = new Date();         
DateFormat shortDateFormat = DateFormat.getDateTimeInstance(  DateFormat.SHORT, DateFormat.SHORT);       
   DateFormat mediumDateFormat = DateFormat.getDateTimeInstance( DateFormat.MEDIUM,DateFormat.MEDIUM);         
   DateFormat longDateFormat =         DateFormat.getDateTimeInstance(         DateFormat.LONG,         DateFormat.LONG);        
   DateFormat fullDateFormat =         DateFormat.getDateTimeInstance(         DateFormat.FULL,         DateFormat.FULL);        
   System.out.println(shortDateFormat.format(date));         
   System.out.println(mediumDateFormat.format(date));         
   System.out.println(longDateFormat.format(date));         
   System.out.println(fullDateFormat.format(date));    
 }
 
}
 

注意我们在对 getDateTimeInstance的每次调用中都传递了两个值. 第一个参数是日期风格, 而第二个参数是时间风格. 它们都是基本数据类型int(整型). 考虑到可读性, 我们使用了DateFormat 类提供的常量: SHORT, MEDIUM, LONG, 和 FULL. 要知道获取时间和日期格式化过程的更多的方法和选项, 请看Sun 公司Web 站点上的解释.


[java]  view plain copy
  1. import java.text.DateFormat;  
  2. import java.util.Date;  
  3. public class date {  
  4.     public static void main(String[] args){  
  5.         Date date=new Date();  
  6.         DateFormat fulldateformat=DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);  
  7.         System.out.println(fulldateformat.format(date));  
  8.     }  
  9. }  
输出结果为2013年3月23日 星期六 下午05时23分11秒 CST
### Java 控制Date 字段格式输出方法 在 Java 控制中,若需要将 `Date` 类型字段以指定格式输出,可以使用 `SimpleDateFormat` 或 `DateTimeFormatter` 来实现。这些工具允许将 `Date` 对象格式化为字符串,以便在接口响应或日志输出中展示特定格式的日期时间信息。 #### 使用 `SimpleDateFormat` 格式输出 `SimpleDateFormat` 是 `java.text` 包中的,支持灵活的日期时间格式化方式。例如: ```java import java.text.SimpleDateFormat; import java.util.Date; public class DateController { public String getFormattedDate() { Date now = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return formatter.format(now); } } ``` 此方式适用于 `java.util.Date` 和 `java.sql.Timestamp` 类型格式输出[^1]。 #### 使用 `DateTimeFormatter` 格式输出 对于使用 Java 8 及以上版本的项目,推荐使用 `java.time` 包中的 `LocalDateTime` 和 `DateTimeFormatter`,它们提供了更强大的日期时间处理能力,并且是线程安全的。例如: ```java import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class DateController { public String getFormattedDate() { LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); return now.format(formatter); } } ``` 该方法适用于 `LocalDateTime`、`LocalDate` 等新日期时间 API 的格式输出[^4]。 #### 在 JSON 序列化中格式化 `Date` 字段 当 `Date` 字段作为 REST 接口返回值时,可以通过注解控制其输出格式。例如,使用 `@JsonFormat` 注解指定日期时间格式: ```java import com.fasterxml.jackson.annotation.JsonFormat; import java.util.Date; public class User { @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date createTime; // getter and setter } ``` 此方式确保 `Date` 类型字段在转换为 JSON 字符串时自动按照指定格式输出[^4]。 #### 使用 `TypeHandler` 自定义 MyBatis 输出格式 如果在 MyBatis 中希望直接将数据库时间字段转换为字符串格式,可以通过自定义 `TypeHandler` 实现。例如: ```java import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Date; public class DateToStringTypeHandler extends BaseTypeHandler<String> { private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, parameter); } @Override public String getNullableResult(ResultSet rs, String columnName) throws SQLException { Date date = rs.getDate(columnName); return date != null ? dateFormat.format(date) : null; } @Override public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { Date date = rs.getDate(columnIndex); return date != null ? dateFormat.format(date) : null; } @Override public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { Date date = cs.getDate(columnIndex); return date != null ? dateFormat.format(date) : null; } } ``` 在 XML 映射文件中使用该 `TypeHandler`: ```xml <resultMap id="userResultMap" type="User"> <result property="createTime" column="create_time" typeHandler="com.example.DateToStringTypeHandler"/> </resultMap> ``` 通过上述方式,可以在 MyBatis 查询结果中直接返回格式化后的日期字符串[^1]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值