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

来源:互联网 发布:淘宝代运营合作 编辑:程序博客网 时间:2024/06/06 07:48

  1.   
  2. public class DateParserT {  
  3.   
  4.     /** 
  5.      * Date 与  String、long 的相互转换 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.           
  10.         Date dt =new Date();  
  11.         System.out.println(dt); //格式: Wed Jul 06 09:28:19 CST 2016  
  12.           
  13.         //格式:2016-7-6  
  14.         String formatDate = null;  
  15.         formatDate = DateFormat.getDateInstance().format(dt);  
  16.         System.out.println(formatDate);    
  17.           
  18.         //格式:2016年7月6日 星期三  
  19.         formatDate = DateFormat.getDateInstance(DateFormat.FULL).format(dt);  
  20.         System.out.println(formatDate);  
  21.           
  22.         //格式 24小时制:2016-07-06 09:39:58  
  23.         DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制;  
  24.         formatDate = dFormat.format(dt);  
  25.         System.out.println(formatDate);  
  26.           
  27.         //格式12小时制:2016-07-06 09:42:44  
  28.         DateFormat dFormat12 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //hh表示12小时制;  
  29.         formatDate = dFormat12.format(dt);  
  30.         System.out.println(formatDate);  
  31.           
  32.         //格式去掉分隔符24小时制:20160706094533  
  33.         DateFormat dFormat3 = new SimpleDateFormat("yyyyMMddHHmmss");  
  34.         formatDate = dFormat3.format(dt);  
  35.         System.out.println(formatDate);  
  36.           
  37.         //格式转成long型:1467770970  
  38.         long lTime = dt.getTime() / 1000;  
  39.         System.out.println(lTime);  
  40.           
  41.         //格式long型转成Date型,再转成String:  1464710394 -> ltime2*1000 -> 2016-05-31 23:59:54  
  42.         long ltime2 = 1464710394;  
  43.         SimpleDateFormat lsdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  44.         Date lDate = new Date(ltime2*1000);  
  45.         String lStrDate = lsdFormat.format(lDate);  
  46.         System.out.println(lStrDate);  
  47.           
  48.         //格式String型转成Date型:2016-07-06 10:17:48 -> Wed Jul 06 10:17:48 CST 2016  
  49.         String strDate = "2016-07-06 10:17:48";  
  50.         SimpleDateFormat lsdStrFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  51.         try {  
  52.             Date strD = lsdStrFormat.parse(strDate);  
  53.             System.out.println(strD);  
  54.         } catch (ParseException e) {  
  55.             e.printStackTrace();  
  56.         }  
  57.   
  58.     }  
  59.   



    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();
            }

原创粉丝点击