Date 形式转换

下面是一个简单代码


package com.test_1.jinjielianxi;



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


public class Datezhuanhuan{
   
   public static void main(String[] args) throws ParseException {
       
// 使用format()方法将日期转换为指定格式的文本
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       
// 创建Date对象,表示当前时间
       
       Date now =new  Date();
       // 调用format()方法,将日期转换为字符串并输出
System.out.println(sdf1.format(now));
System.out.println(sdf2.format(now));
System.out.println(sdf3.format(now));


// 使用parse()方法将文本转换为日期
String d = "2014-6-1 21:05:36";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       
        // 调用parse()方法,将字符串转换为日期
Date date =sdf.parse(d);
       
System.out.println(date);
}



}
### Date 格式转换的方法 在 Java 中,`Date` 类型可以通过 `SimpleDateFormat` 进行格式化操作。以下是关于如何将整数类型的时间戳转换为 `Date` 对象以及对其进行格式化的具体方法。 #### 时间戳转 Date 并格式化输出 当需要将 Integer 或 Long 类型的时间戳转换为 `Date` 对象时,可以利用 `new Date(long timestamp)` 构造函数完成此过程[^1]。随后,使用 `SimpleDateFormat` 来定义所需的日期格式并将其应用到 `Date` 对象上: ```java import java.text.SimpleDateFormat; import java.util.Date; public class TimestampToDateExample { public static void main(String[] args) { long timestamp = System.currentTimeMillis(); // 获取当前时间戳 (毫秒级) // 将时间戳转换Date 对象 Date date = new Date(timestamp); // 定义日期格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); // 格式化输出 String formattedDate = sdf.format(date); System.out.println(formattedDate); // 输出格式化后的字符串 } } ``` 上述代码展示了如何将一个标准的 Unix 时间戳(单位为毫秒)转化为可读性强的日期形式,并按照特定模式打印出来[^2]。 #### Spring MVC 中自定义 Date 参数绑定器 如果是在 Web 开发环境下工作,则可能面临从前端传递不同样式的日期字符串给后端服务的问题。此时可以在控制器类中加入如下初始化逻辑以支持多样的输入样式解析成统一的标准 `Date` 实例[^3]: ```java import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import java.beans.PropertyEditorSupport; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; @InitBinder protected void initBinder(WebDataBinder binder){ DateFormat[] formats = { new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyy/MM/dd"), new SimpleDateFormat("yyyy:MM:dd"), new SimpleDateFormat("yyyy年MM月dd日") }; binder.registerCustomEditor(Date.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { for(DateFormat format : formats){ try{ setValue(format.parse(text)); return; // 成功匹配则退出循环 }catch(ParseException e){} } throw new IllegalArgumentException("无法识别的日期格式:" + text); } @Override public String getAsText(){ Date value = (Date)this.getValue(); return value != null ? new SimpleDateFormat("yyyy-MM-dd").format(value): ""; } }); } ``` 以上片段实现了自动适配多种常见日期表达方式的功能,在接收到不符合预期的数据之前不会抛出异常而是尝试逐一验证直至找到合适的解释方案为止。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值