標籤:throw info 1.0 bank main pre name 字元 mat
package com.banksteel.openerp.commons.utils;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/** * @description:時間轉換 * @projectName:openerp-commons * @className:TimeTransf.java * @author: * @createTime:2016年7月28日 下午6:06:53 * @version 1.0 */public class TimeTransf{/** * @description:字串型時間轉換成長整型時間 * @param time 字串型時間 * @param format 時間表現形式 * @return 長整型時間 * @throws ParseException * @author: * @createTime:2016年7月28日 下午6:07:33 */public static Long StringToLong(String time, String format) throws ParseException{if(time!=null){return new SimpleDateFormat(format).parse(time).getTime();}return null;}/** * @description:長整型時間轉換成字串型時間 * @param time 長整型時間 * @param format 時間表現形式 * @return 字串型時間 * @author: * @createTime:2016年7月28日 下午6:10:50 */public static String LongToString(Long time, String format){if(time!=null){return new SimpleDateFormat(format).format(new Date(time));}return null;}public static void main(String[] args) throws ParseException{Long currentTimeMillis = System.currentTimeMillis();String str = TimeTransf.LongToString(currentTimeMillis, "yyyy-MM-dd HH:mm:ss.SSS");System.out.println(str);Long t = TimeTransf.StringToLong(str, "yyyy-MM-dd HH:mm:ss.SSS");System.out.println(t);String str1 = TimeTransf.LongToString(currentTimeMillis, "yyyy-MM-dd HH:mm:ss");System.out.println(str1);Long t1 = TimeTransf.StringToLong(str1, "yyyy-MM-dd HH:mm:ss");System.out.println(t1);String str2 = TimeTransf.LongToString(currentTimeMillis, "yyyy-MM-dd");System.out.println(str2);Long t2 = TimeTransf.StringToLong(str2, "yyyy-MM-dd");System.out.println(t2);}}
2
java時間Long和String相互轉化