1 package com.test; 2 import org.apache.commons.lang.StringUtils; 3 import org.junit.Test; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 7 /** 8 * @author ceshi 9 * @Title: SimpleDate 10 * @Package test 11 * @Description: SimpleDate 12 * @date 2018/5/3016:32 13 */ 14 public class JunitSimpleDate { 15 16 public static String DATE_YYYY_MM_DD = "yyyy-MM-dd"; 17 18 public static String DATE_Y_M_DDHHMMSS = "yyyy-MM-dd HH:mm:ss"; 19 20 @Test 21 public void test() { 22 System.out.println(convert2long("2018-05-30",DATE_YYYY_MM_DD)); 23 System.out.println(convert2String(curTimeMillis(),DATE_Y_M_DDHHMMSS)); 24 } 25 26 /** 27 * 将日期格式的字符串转换为长整型 28 * @param date 29 * @param format 30 * @return 31 */ 32 public static long convert2long(String date, String format) { 33 try { 34 if (StringUtils.isNotBlank(date)&&StringUtils.isNotBlank(format)) { 35 SimpleDateFormat sf = new SimpleDateFormat(format); 36 return sf.parse(date).getTime(); 37 } 38 } catch (Exception e) { 39 e.printStackTrace(); 40 } 41 return 0l; 42 } 43 44 /** 45 * 将长整型数字转换为日期格式的字符串 46 * @param time 47 * @param format 48 * @return 49 */ 50 public static String convert2String(long time, String format) { 51 if (time > 0l&&StringUtils.isNotBlank(format)) { 52 SimpleDateFormat sf = new SimpleDateFormat(format); 53 Date date = new Date(time); 54 return sf.format(date); 55 } 56 return ""; 57 } 58 59 /** 60 * 获取当前系统的日期 61 * @return 62 */ 63 public static long curTimeMillis() { 64 return System.currentTimeMillis(); 65 } 66 67 68 }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/18549.html