SimpleDateFormat日期转对象与字符串转日期

/**
 * @program: luceneTest
 * @description: 日期转换
 * @author: LingFeng
 * @create: 2018-12-10 09:43
 */
public class DataUtils {
    //日期转对象
    public static String formatData(String format,Date date){
        SimpleDateFormat sim=new SimpleDateFormat(format);//时间格式化
        String result=sim.format(date);//后面转化时间戳一样的
        System.out.println(result);
       return result;
    }
  //字符串转日期
    public static Date formatString(String format,String date){
        SimpleDateFormat sim=new SimpleDateFormat(format);
        Date d= null;
        try {
            d = sim.parse(date);
            System.out.println(d);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return d;
    }
    public static void main(String[] args) {
        System.out.println(new Date().getTime());
        System.out.println(System.currentTimeMillis());//获取当前时间戳与new Date().getTime()一样
        formatData("yyyy-mm-dd  HH:mm:ss",new Date());
        formatString("yyyy-mm-dd  HH:mm:ss","2018-17-10  10:17:25");

    }


}

运行结果

猜你喜欢

转载自blog.csdn.net/linlinlinfeng/article/details/84936159