java時間Long和String相互轉化

來源:互聯網
上載者:User

標籤: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相互轉化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.