如何将长整数转换为带有“ dd / MM / yyyy”的日期格式 [英] How to convert a long to a Date with "dd/MM/yyyy" format

查看:105
本文介绍了如何将长整数转换为带有“ dd / MM / yyyy”的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Long类型的变量。

Long longDate = 20180201110400

I have a variable of type Long.
Long longDate = 20180201110400

它表示如下: 2018/02/01 11:04:00

It represents this: 2018/02/01 11:04:00

I要转换格式和变量类型,如下所示:

I want to convert the format and variable type like below:

格式应为 dd / MM / yyyy,类型应为日期。我该怎么办?

Format should be "dd/MM/yyyy" and type should be Date. How can I do that?

推荐答案

您可以先将long转换为Date对象,然后再将其转换为所需的对象格式。下面是代码示例。

You can covert the long to a Date object first then you can further convert it to your desired format. Below is the code sample.

Long longDate = 20180201110400L;

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

Date date  = dateFormat.parse(longDate.toString());

System.out.println("Date : "+date);

SimpleDateFormat dateFormatNew = new SimpleDateFormat("dd/MM/yyyy");

String formattedDate = dateFormatNew.format(date);

System.out.println("Formatted date : "+formattedDate);

这篇关于如何将长整数转换为带有“ dd / MM / yyyy”的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆