Java String与时间相互转换

String时间互转

两种办法:

  1. 使用DateFormat
  2. 使用DateTimeFormatter
一、DateFormat
  1. String转时间

    DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
    DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String s ="2020-05-09";
    String S2= "2020-05-10 16:21:02 ";
    Date date = format1.parse(s);
    Date date2 = format2.parse(S2);
    System.out.println("date="+date);
    System.out.println("date2="+date2);
    
    结果:
    date=Sat May 09 00:00:00 CST 2020
    date2=Sun May 10 16:21:02 CST 2020
    
  2. 时间转String

    Date datex = new Date();
    DateFormat format3 = new SimpleDateFormat("yyyy-MM-dd");
    DateFormat format4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("format3="+format3.format(datex));
    System.out.println("format4="+format4.format(datex));
    
    结果:
    format3=2020-05-13
    format4=2020-05-13 13:39:59
    
二、DateTimeFormatter
     // String --> LocalDate
     LocalDate localDate1 = LocalDate.parse("2020-12-07");
     DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
     System.out.println(LocalDate.parse("2019-10-09").format(pattern));
     // String --> LocalTime
     LocalTime localTime = LocalTime.parse("07:43:53");
     // String -->LocalDateTime
     DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
     LocalDateTime localDate = LocalDateTime.parse("2019-12-07 07:43:53",formatter1);
             
     System.out.println("localDate"+localDate);
     System.out.println("localTime"+localTime);
     System.out.println("localDate"+localDate);


------------------
localDate 2019-12-07T07:43:53
localTime 07:43:53
localDate 2019-12-07T07:43:53
    

设置时区

TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,Date与String之间的转换是非常常见的操作,因为不同的应用程序或组件使用的日期格式可能是不同的。因此,开发者需要使用工具类来处理它们之间的转换。 下面是一个可用于Date与String相互转换Java工具类: public class DateUtil { // 将日期转换为字符串 public static String dateToString(Date date, String format) { SimpleDateFormat dateFormat = new SimpleDateFormat(format); return dateFormat.format(date); } // 将字符串转换为日期 public static Date stringToDate(String str, String format) throws ParseException { SimpleDateFormat dateFormat = new SimpleDateFormat(format); return dateFormat.parse(str); } } 在上面的代码中,dateToString方法用于将日期对象转换为字符串,并传入一个特定的日期格式。stringToDate方法用于将字符串转换为日期对象,并同样需要传入日期格式。它们都使用Java中的SimpleDateFormat类来实现这些转换。 使用这个工具类,可以很方便地将日期对象转换为不同的字符串格式,或将不同格式的字符串转换为日期对象。例如: Date now = new Date(); String nowStr = DateUtil.dateToString(now, "yyyy-MM-dd HH:mm:ss"); System.out.println(nowStr); // 将输出当前时间的字符串表示,格式为"yyyy-MM-dd HH:mm:ss" String str = "2021-01-01 00:00:00"; Date newYear = DateUtil.stringToDate(str, "yyyy-MM-dd HH:mm:ss"); System.out.println(newYear); // 将输出日期对象表示"2021-01-01 00:00:00"

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值