怎么将String转换为Date,怎么将Date转换为String

String转换为Date

使用 SimpleDateFormat 类(旧的日期时间 API)

  • SimpleDateFormat 是 java.text 包中的一个类,用于格式化和解析日期。它的构造函数接受一个日期格式的字符串作为参数,用于指定要解析的日期字符串的格式。例如,“yyyy - MM - dd HH:mm:ss” 是一种常见的日期时间格式,其中 “yyyy” 表示四位年份,“MM” 表示两位月份,“dd” 表示两位日期,“HH” 表示 24 小时制的小时数,“mm” 表示分钟数,“ss” 表示秒数。
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Main {
        public static void main(String[] args) {
            try {
                String dateString = "2024-11-07 12:30:00";
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = sdf.parse(dateString);
                System.out.println(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

  • 在上述代码中,首先定义了一个日期字符串dateString,其格式为 “yyyy - MM - dd HH:mm:ss”。然后创建了一个 SimpleDateFormat 对象sdf,并将日期格式字符串作为参数传入。最后,调用sdf.parse(dateString)方法将日期字符串解析为 Date 类型的对象。如果日期字符串的格式与指定的格式不匹配,会抛出 ParseException 异常,所以需要进行异常处理。

  • 使用 DateTimeFormatter 类(新的日期时间 API)

    • 在 Java 8 及以后的版本中,引入了新的日期时间 API(java.time 包),其中 DateTimeFormatter 类用于格式化和解析日期时间。它提供了更强大和灵活的功能。
      import java.time.LocalDateTime;
      import java.time.format.DateTimeFormatter;
      
      public class Main {
          public static void main(String[] args) {
              String dateString = "2024-11-07T12:30:00";
              DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
              LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
              System.out.println(dateTime);
          }
      }

    • 在这个示例中,定义了一个日期时间字符串dateString,其格式符合 ISO_LOCAL_DATE_TIME 标准格式(“yyyy - MM - ddTHH:mm:ss”)。创建了一个 DateTimeFormatter 对象formatter,并使用LocalDateTime.parse(dateString, formatter)方法将日期时间字符串解析为 LocalDateTime 类型的对象。LocalDateTime 是新日期时间 API 中的一个类,用于表示日期和时间,没有时区信息。如果日期时间字符串的格式与指定的格式不匹配,会抛出 DateTimeParseException 异常。

将 Date 类型转换为String

  • 使用 SimpleDateFormat 类(旧的日期时间 API)
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Main {
        public static void main(String[] args) {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = sdf.format(date);
            System.out.println(dateString);
        }
    }

  • 在上述代码中,首先创建了一个 Date 类型的对象date(这里使用了无参数构造函数,会获取当前日期和时间)。然后创建了一个 SimpleDateFormat 对象sdf,并指定了日期格式。最后,调用sdf.format(date)方法将 Date 类型的对象格式化为指定格式的字符串,并将结果存储在dateString变量中。

  • 使用 DateTimeFormatter 类(新的日期时间 API)

    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    
    public class Main {
        public static void main(String[] args) {
            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String dateString = now.format(formatter);
            System.out.println(dateString);
        }
    }

  • 在这个示例中,首先使用LocalDateTime.now()获取当前日期和时间,得到一个 LocalDateTime 类型的对象now。然后通过DateTimeFormatter.ofPattern("yyyy - MM - dd HH:mm:ss")创建一个指定格式的 DateTimeFormatter 对象formatter。最后,调用now.format(formatter)方法将 LocalDateTime 对象格式化为指定格式的字符串,并存储在dateString变量中。新的日期时间 API 在处理日期时间相关操作时更加灵活和方便,推荐在 Java 8 及以上版本中使用。

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小懒懒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值