获取系统时间及Timestamp和String互转

1、获取当前时间。

1、通过Date类来获取当前时间。
        //format = 2022-11-10 23:47:49
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        System.out.println("format = " + format);

2、通过System类中的currentTimeMillis方法来获取当前时间。
        //format1 = 2022-11-10 23:47:49
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format1 = dateFormat1.format(System.currentTimeMillis());
        System.out.println("format1 = " + format1);
        
3、通过Date类来获取当前日期。
        //2022年十一月10日
        Date date1 = new Date();
        System.out.println(String.format("%tY", date1) + "年" + String.format("%tB", date1) + String.format("%te", date1) + "日");

4、通过Calendar类来获取当前时间。
        //2022/10/10 23:47:49
        Calendar instance = Calendar.getInstance();
        System.out.println(instance.get(Calendar.YEAR) + "/" + instance.get(Calendar.MONTH) + "/" + instance.get(Calendar.DATE) + " " + instance.get(Calendar.HOUR_OF_DAY) + ":" + instance.get(Calendar.MINUTE) + ":" + instance.get(Calendar.SECOND));
        

2、获取系统当前时间,Timestamp和String互转

方法1Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		
方法2Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
//Timestamp转化为String,定义格式,不显示毫秒
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        String str = dateFormat.format(timestamp);

//String转化为Timestamp
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        Timestamp timestamp = Timestamp.valueOf(format);