Java中Date日期格式的各种转换

本文介绍如何使用Java进行日期和时间的格式化操作,包括Date与String、long之间的相互转换。文章详细展示了不同日期格式的输出方式,并提供了字符串转日期、long类型时间戳转日期等实用代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

      
    public class DateParserT {  
      
        /** 
         * Date 与  String、long 的相互转换 
         * @param args 
         */  
        public static void main(String[] args) {  
              
            Date dt =new Date();  
            System.out.println(dt); //格式: Wed Jul 06 09:28:19 CST 2016  
              
            //格式:2016-7-6  
            String formatDate = null;  
            formatDate = DateFormat.getDateInstance().format(dt);  
            System.out.println(formatDate);    
              
            //格式:2016年7月6日 星期三  
            formatDate = DateFormat.getDateInstance(DateFormat.FULL).format(dt);  
            System.out.println(formatDate);  
              
            //格式 24小时制:2016-07-06 09:39:58  
            DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制;  
            formatDate = dFormat.format(dt);  
            System.out.println(formatDate);  
              
            //格式12小时制:2016-07-06 09:42:44  
            DateFormat dFormat12 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //hh表示12小时制;  
            formatDate = dFormat12.format(dt);  
            System.out.println(formatDate);  
              
            //格式去掉分隔符24小时制:20160706094533  
            DateFormat dFormat3 = new SimpleDateFormat("yyyyMMddHHmmss");  
            formatDate = dFormat3.format(dt);  
            System.out.println(formatDate);  
              
            //格式转成long型:1467770970  
            long lTime = dt.getTime() / 1000;  
            System.out.println(lTime);  
              
            //格式long型转成Date型,再转成String:  1464710394 -> ltime2*1000 -> 2016-05-31 23:59:54  
            long ltime2 = 1464710394;  
            SimpleDateFormat lsdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
            Date lDate = new Date(ltime2*1000);  
            String lStrDate = lsdFormat.format(lDate);  
            System.out.println(lStrDate);  
              
            //格式String型转成Date型:2016-07-06 10:17:48 -> Wed Jul 06 10:17:48 CST 2016  
            String strDate = "2016-07-06 10:17:48";  
            SimpleDateFormat lsdStrFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
            try {  
                Date strD = lsdStrFormat.parse(strDate);  
                System.out.println(strD);  
            } catch (ParseException e) {  
                e.printStackTrace();  
            }  
      
        }  
      
    }  



   
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       
            String timeStr = sdf.format(new Date());
            System.out.println("当前时间为:"+timeStr);
            String fuck = timeStr.substring(5,timeStr.length());
            System.out.println(fuck);
            
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年M月d日");
            try {
                Date d = sdf2.parse("2013年01月06日");
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                System.out.println(sdf.format(d));
                System.out.println(d.getDate());
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
            SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
            Date  date = sdf3.parse("2017-09-01");
            sdf3=new SimpleDateFormat("MM-dd");
            String shit = sdf3.format(date);
            System.out.println(shit);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

 

 

<think>我们正在处理一个关于Java日期格式转换的问题。用户希望看到Java日期格式转换的示例。 根据引用[1],我们有一个使用java.sql.Date的示例,但用户可能需要更通用的日期格式转换,比如使用java.text.SimpleDateFormat或Java 8的java.time.format.DateTimeFormatter。 由于用户没有指定具体使用的Java版本,我们将提供两种方式:传统的SimpleDateFormat(Java 8之前)和新的DateTimeFormatter(Java 8及以后)。 步骤: 1. 使用SimpleDateFormat(传统方式,适用于Java 7及之前) 2. 使用DateTimeFormatter(Java 8及以上) 注意:用户的问题是关于日期格式转换,所以我们将展示如何将日期从一种字符串格式转换为另一种字符串格式,以及如何将Date对象格式化为字符串。 引用[1]中使用了java.sql.Date,但通常我们使用java.util.Date进行日期操作。不过,java.sql.Datejava.util.Date的子类,但主要用于数据库。因此,在示例中,我们将使用java.util.Date。 另外,引用[2]和[3]是关于PHP的,与当前问题无关,因此忽略。 我们将提供两个示例: 示例1:使用SimpleDateFormat将字符串解析为Date对象,再将Date对象格式化为另一种字符串。 示例2:使用Java 8的LocalDateDateTimeFormatter进行同样的操作。 同时,考虑到用户可能希望看到当前日期的转换,我们也会使用当前日期作为示例。 注意:SimpleDateFormat是线程不安全的,而DateTimeFormatter是线程安全的。 示例代码:</think>### Java 日期格式转换示例 在 Java 中进行日期格式转换主要使用 `SimpleDateFormat` 类(Java 8 之前)或 `DateTimeFormatter` 类(Java 8+)。以下是两种方法的详细示例: #### 方法 1:使用 `SimpleDateFormat`(传统 API) ```java import java.text.SimpleDateFormat; import java.util.Date; public class DateConversionExample { public static void main(String[] args) { // 原始日期格式 String originalDate = "2023-08-15"; try { // 步骤 1:解析原始日期字符串 SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = inputFormat.parse(originalDate); // 步骤 2:转换为目标格式 SimpleDateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值