Java 日期与数字转换

 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 }

 

posted on 2018-05-30 16:42 【cosmo】 阅读( ...) 评论( ...) 编辑 收藏
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值