-3

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

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

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

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
fuat
  • 1,484
  • 2
  • 19
  • 25
  • A `Date` cannot have a format.See [How can I change the date format in Java?](https://stackoverflow.com/questions/3469507/how-can-i-change-the-date-format-in-java) Furthermore the `Date` class is long outdated, so you shouldn’t want to have one. I recommend you use [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/), instead. – Ole V.V. Feb 01 '18 at 09:17
  • Better sources for my claim that a `Date` cannot have a format: (1) [Blog entry All about java.util.Date](https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/). (2) [Question: Change the format of Date Java](https://stackoverflow.com/questions/18628059/change-the-format-of-date-java). – Ole V.V. Feb 01 '18 at 09:30
  • For you and anyone interested in using `java.time` (which I warmly recommend) I have provided [a new answer to the linked question](https://stackoverflow.com/a/48561162/5772882). – Ole V.V. Feb 01 '18 at 11:29

3 Answers3

3

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);
shmosel
  • 49,289
  • 6
  • 73
  • 138
Amit Bera
  • 7,075
  • 1
  • 19
  • 42
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class as the first option and without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Feb 01 '18 at 09:11
0

cast Long to Date:

Long longDate = 20180201110400L;
String dateAsString = String.valueOf(longDate);
Date date = new SimpleDateFormat("yyyyMMddHHmmss").parse(dateAsString);

cast Date to String with "dd/MM/yyyy" format:

String formattedDate = new SimpleDateFormat("dd/MM/yyyy").format(date);
MrOrange__
  • 26
  • 3
-1

To convert in any standard date format, we can use SimpleDateFormat class. See the below snippet

Long longDate = new Date().getTime();   
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String formatedDate = dateFormat.format(longDate);

System.out.println(formatedDate);

Output : 01/02/2018

Indra K
  • 114
  • 1
  • 2
  • 1
    Your input `longDate` is not comparable with the OP's input `Long`. Therefore your answer does not answer his question – Robin Topper Feb 01 '18 at 09:09
  • Thanks Buddy for comment. My intention is, if we have date in format of Long, we can convert into standard format. – Indra K Feb 01 '18 at 10:34
  • Your code snippet as it stands gives the expected result today. If I run it tomorrow and the OP’s `long` is still 20180201110400, it will no longer give 01/02/2018 as it should. And if I feed that long into your code, I get `27/06/2609`, completely wrong. I think you misread the question. – Ole V.V. Feb 01 '18 at 11:37