java编程之怎样把Long转换成Date的日期格式

 时间:2026-02-14 15:13:22

1、Long类型的时间转换为date,可以通过SimpleDateFormat对象对格式进行定义,然后创建一个Date类型的对象封装时间,再通过SimpleDateFormat对象的format(date)方法就可以获取指定的日期格式了。

2、有了上面的介绍,看看我是怎么封装一个简单的Long转换为Date的函数:

    /**

     * 把毫秒转化成日期

     * @param dateFormat(日期格式,例如:MM/ dd/yyyy HH:mm:ss)

     * @param millSec(毫秒数)

     * @return

     */

    private String transferLongToDate(String dateFormat,Long millSec){

     SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);

     Date date= new Date(millSec);

            return sdf.format(date);

    }

3、写一个main函数测试一下我们写的方法:

import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class test {    public static void main(String[] args) throws ParseException {        // TODO Auto-generated method stub        System.out.println(transferLongToDate("MM/dd/yyyy",System.currentTimeMillis()));    }      /**     * 把毫秒转化成日期     * @param dateFormat(日期格式,例如:MM/ dd/yyyy HH:mm:ss)     * @param millSec(毫秒数)     * @return     */    private static String transferLongToDate(String dateFormat,Long millSec){     SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);     Date date= new Date(millSec);            return sdf.format(date);    }}

  • linux查找文件中的关键字
  • linux如何删除文件
  • git如何切换用户
  • notepad格式化json
  • 使用PLSQL连接oracle数据库
  • 热门搜索
    头寸是什么意思 开字加一笔是什么字 什么东西养胃 梦见狗咬手是什么意思 时间煮雨是什么意思 肾盂分离是什么意思 迷惘是什么意思 二百五是什么意思 支原体衣原体是什么 什么手游好玩