zoukankan      html  css  js  c++  java
  • simpleDateFormat日期格式转换

     

    1-------------------------------------------------------------------------------------

    package com.neusoft.date;

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

    public class SimpleDateFormat01
    {

        public static void main(String[] args) throws ParseException
        {
            // TODO Auto-generated method stub
          String str = "2009-02-15 09:21:35.345";
          SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
          SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒SSS毫秒");
          Date date = sdf1.parse(str);//提取格式中的日期
          System.out.println("转换之前的日期:"+date);
          String newStr = sdf2.format(date); //改变格式
          System.out.println("转换之后的日期:"+newStr);
           
        }

    }

    2-------------------------------------------------------------------------------------

    package com.neusoft.date;

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

    public class SimpleDateFormatDemo02
    {
     public static void main(String []args) throws ParseException{
        String str = "2009-02-15 09:21:35.345";//接收參数
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = sdf.parse(str);//提取格式中的日期
        String str1 = sdf1.format(date);
        System.out.println(date);
        System.out.println(str1);
       
     }
    }

    3-------------------------------------------------------------------------------------

    package com.neusoft.date;

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

    public class SimpleDateFormatDemo03
    {
        SimpleDateFormat sdf = null;
        //2012-03-05
        public String DateFormat(){
            this.sdf = new SimpleDateFormat("yyyy-MM-dd");
            String str = this.sdf.format(new Date());
            return str;
        }
        //2012-03-05 05:27:15:390
        public String DateFormat1(){
            this.sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS");
            String str = this.sdf.format(new Date());
            return str;
        }
        //2012年03月05日
        public String dateNewFormat(){
            this.sdf = new SimpleDateFormat("yyyy年MM月dd日");
            String str = this.sdf.format(new Date());
            return str;
        }
        //2012年03月05日05时26分19秒437毫秒
        public String dateNewFormat1(){
            this.sdf = new SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒SSS毫秒");
            String str = this.sdf.format(new Date());
            return str;
        }
        public String dateNewFormat2(){
            this.sdf = new SimpleDateFormat("yyyyMMddhhmmssSSS");
            String str = this.sdf.format(new Date());
            return str;
        }
     public static void main(String []args) throws ParseException{
         SimpleDateFormatDemo03 ss= new SimpleDateFormatDemo03();
         System.out.println(ss.DateFormat());
         System.out.println(ss.DateFormat1());
         System.out.println(ss.dateNewFormat());
         System.out.println(ss.dateNewFormat1());
         System.out.println(ss.dateNewFormat2());
     }
    }

  • 相关阅读:
    C#将一个字符串数组的元素的顺序进行反转
    C#找出100内所有的素数/质数
    C#流程控制for循环语句,水仙花数。
    C# 常用的操作文件夹的方法
    Element UI
    JS
    JS
    PHP基础算法
    js实现csv下载
    el-dialog“闪动”解决办法
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4565934.html
Copyright © 2011-2022 走看看