SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS")时间转换问题

SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS")时间转换问题

  • 程序代码:

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Time {
    public static void main(String[] args) throws ParseException {
    //定义一个时间字符串
    String date_str = "2016-06-23 09:46:27.000";
    //将时间字符串转换成Date形式,不能够强制转换
    Date date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS").parse(date_str);
    //定义新的字符串str1和str2,将Date型转为String型
    String str1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
    String str2 = (new SimpleDateFormat("yyyy年MM月dd日 ")).format(date);
    
    //取当前时间进行修改,方法1
    String str3 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
    //取当前时间进行修改,方法2,其实和方法1类似
    SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
    String str4 = format.format(new Date());
            System.out.println("str1:   "+str1);
    System.out.println("str2:   "+str2);
    System.out.println("str3:   "+str3);
    System.out.println("str4:   "+str4);
        }   
    }
  • 输出:

猜你喜欢

转载自www.cnblogs.com/renxiuxing/p/9194003.html