1

I'm converting a VB.Net program to Java and I have to convert following line

Dim s = Now.ToString("yyyy-MM-dd")

I have written following Java code

Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String s = sdf.format(d);

that I can write on one line

String s = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

What is the shorter code in Java that works in Java 6 and greater ?

Is there some Java codes that generates date formatted string in calling a method of Date object (example: Date.format("yyyy-MM-dd")) ?

tramex
  • 149
  • 6
  • A date format object is expensive to create. This way you have a chance to re-use it. But take care - it's not thread-safe. Maybe this answers the question why there is no shorter alternative. – Mick Dec 05 '18 at 09:32
  • 2
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 05 '18 at 09:54
  • 2
    Don’t strive for short code. Strive for clearly readable code instead. Sometimes they will happen to be the same, sometimes the shorter code is less readable. – Ole V.V. Dec 05 '18 at 09:54
  • `String s = Now.ToString("yyyy-MM-dd")`, that is not vb... – Trevor Dec 05 '18 at 12:19
  • yes it is not VB. I will correct that. – tramex Dec 05 '18 at 12:44

2 Answers2

2

What is the shorter code in Java that works in Java 6 and greater ?

I’d like to answer the “Java 6 and greater” part: The code in deHaar’s answer does. My version would be either

    String now = LocalDate.now(ZoneId.systemDefault()).toString();

or

    DateTimeFormatter customFormatter = new DateTimeFormatter.ofPattern("uuuu-MM-dd");
    String now = LocalDate.now(ZoneId.systemDefault()).format(customFormatter);
  • In Java 8 and later and on newer Android devices (from API level 26) java.time, the modern API, comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I want to vote up but I don't have sufficient reputations ! Thanks for your answer for any Java version. – tramex Dec 05 '18 at 10:21
1

From Java 8 on, there is java.time with several useful classes.

If you may / can use Java 8 or higher, then you can do the following:

String now = LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE);

which will output 2018-12-05 today in my time zone (may differ in yours).

You just need to

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

in order to make it work. You can as well apply custom formattings with

DateTimeFormatter customFormatter = new DateTimeFormatter.ofPattern("yyyy-MM-dd");
String now = LocalDate.now().format(customFormatter);
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 1
    For the first snippet you may alternatively use just `LocalDate.now().toString()`. I recommend giving explicit time zone to `now`, even if you just do `LocalDate.now(ZoneId.systemDefault())`. It more clearly states what you expect to get. – Ole V.V. Dec 05 '18 at 09:52
  • I want to vote up but I don't have sufficient reputations ! Thanks for your answer. – tramex Dec 05 '18 at 10:20
  • You can accept it as the correct answer... No problem if you cannot upvote it, I remember having been in that situation some months ago ;-) – deHaar Dec 05 '18 at 10:21
  • I have accepted it but it is refused until I have 15 reputations ! Sorry – tramex Dec 05 '18 at 12:50
  • Again no problem, you can upvote and/or accept the answers given by @OleV.V. and me later when you have enough reputation. – deHaar Dec 05 '18 at 12:51