1

I'm using Joda Time to remove seconds from a date.

Firstly, I have this kind of Date:

Thu Nov 05 00:00:00 CET 2020

I pick this date from a local db using this method:

public static Date dateTimeFromString(String s) {

    String pattern = "yyyy-MM-dd";
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    Date d= null;
    try {
        d = sdf.parse(s);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return d;

From this date I would reach a form like this:

Thu Nov 05 CET 2020

or even better like this:Thu Nov 05 2020.

Now, I'm trying to use this method from Joda Time:

private Date dateWitoutTime(Date date){
    return new LocalDate(date).toDate();
}

but still getting

Thu Nov 05 00:00:00 CET 2020

Any help? Thank you.

ThatsSamu
  • 61
  • 7
  • 4
    Your method is returning a `Date`. The output appears to be the `.toString()` of the `Date`. A `Date` includes time. Do you really want to return a `Date` or a `String` from `dateWithoutTime(...)`? – Jamie Bisotti Nov 04 '20 at 16:41
  • I want a `String`, but I don't want the `HH:mm:ss`. I used that method because It is from `Joda Time` – ThatsSamu Nov 04 '20 at 16:49
  • 1
    There is no point in removing the time by turning it into a LocalDate and then turning into a Date again since Date always includes time even if its 00:00. You either keep the LocalDate object and work with that or you specify the desired format when printing out the Date object – Dennishofken Nov 04 '20 at 16:50
  • Does this somehow answer your question? [want current date and time in “dd/MM/yyyy HH:mm:ss.SS” format](https://stackoverflow.com/questions/8745297/want-current-date-and-time-in-dd-mm-yyyy-hhmmss-ss-format). – Ole V.V. Nov 04 '20 at 18:24

3 Answers3

4

Do not contaminate the clean java.time API with the legacy error-prone java.util date-time API. You can do it as follows using the modern date-time API:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now.format(DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm", Locale.ENGLISH)));
        System.out.println(now.format(DateTimeFormatter.ofPattern("EEE MMM dd uuuu", Locale.ENGLISH)));

        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z uuuu", Locale.ENGLISH);
        ZonedDateTime givenDateTime = ZonedDateTime.parse("Thu Nov 05 00:00:00 CET 2020", dtfInput);

        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("EEE MMM dd z uuuu", Locale.ENGLISH);
        System.out.println(givenDateTime.format(dtfOutput));
        System.out.println(ZonedDateTime.now(ZoneId.of("Europe/Paris")).format(dtfOutput));
    }
}

Output:

04-Nov-2020 17:13
Wed Nov 04 2020
Thu Nov 05 CET 2020
Wed Nov 04 CET 2020

Learn more about the modern date-time API at Trail: Date Time.

Using the legacy API:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) throws ParseException {
        Date now = new Date();
        System.out.println(new SimpleDateFormat("dd-MMM-yyyy HH:mm", Locale.ENGLISH).format(now));
        System.out.println(new SimpleDateFormat("EEE MMM dd yyyy", Locale.ENGLISH).format(now));

        SimpleDateFormat sdfInput = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
        Date givenDateTime = sdfInput.parse("Thu Nov 05 00:00:00 CET 2020");

        SimpleDateFormat sdfOutput = new SimpleDateFormat("EEE MMM dd z yyyy", Locale.ENGLISH);
        sdfOutput.setTimeZone(TimeZone.getTimeZone("CET"));
        System.out.println(sdfOutput.format(givenDateTime));
        System.out.println(sdfOutput.format(now));
    }
}

Output:

04-Nov-2020 17:13
Wed Nov 04 2020
Thu Nov 05 CET 2020
Wed Nov 04 CET 2020
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
2

If you are using Java8+, there's no need for JodaTime.

A Date instance includes the time; there's no getting around that. Maybe you want to return a String instead.

    // Pre-Java8
    private String dateWithoutTime(final Date date) {
        final SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d yyyy");
        return sdf.format(date);
    }

    // Java8+
    private String dateWithoutTime(final LocalDate date) {
        final DateTimeFormat formatter = DateTimeFormatter.ofPattern("EEE MMM d yyyy");
        return date.format(formatter);
    }

    private String dateWithoutTime(final Date date) {
        final LocalDate localDate = convertDateToLocalDate(date);
        return dateWithoutTime(localDate);
    }

    private LocalDate convertDateToLocalDate(final Date date) {
        ...
    }
Jamie Bisotti
  • 2,605
  • 19
  • 23
  • 1
    Only please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Yje OP is already using Joda-Time, which is far superior (Java 8+ or not). Joda-Time includes classes `DateTimeFormat` and `DateTimeFormatter` for formatting dates, so I recommend that you use those instead. Or even better, move on to java.time and it `DateTimeFormatter`. – Ole V.V. Nov 04 '20 at 18:21
  • I disagree with your statement that Joda-Time is superior if you're already using Java8. – Jamie Bisotti Nov 05 '20 at 20:15
  • Thanks for your comment. I’m perfectly fine with the disagreement and very curious about your opinion. I said that Joda-Time was superior *to `SimpleDateFormat`*. Did you misunderstand that? Or do you think that `SimpleDateFormat` has been considerably improved from Java 7 to Java 8, and if so, how? Also thanks for your edit, I consider it a good improvement. – Ole V.V. Nov 05 '20 at 23:11
  • 1
    I did not read you original statement as "Joda-Time is superior to `SimpleDateFormat`"; I took it as saying it should be used even when `java.time` is available. Thanks for the response and feedback. – Jamie Bisotti Nov 06 '20 at 17:32
0

I want a String, …

Well, there’s something wrong about converting from one string format to another string format. Just as you keep numbers in int variables and Boolean values in boolean variables (I hope), you should keep dates in LocalDate variables, either the LocalDate class from Joda-Time or the one from java.time, the modern Java date and time API that has supplanted Joda-Time. But since you ask, then have your method return a String:

private static final DateTimeFormatter dateFormatter
        = DateTimeFormat.forPattern("EEE MMM dd yyyy").withLocale(Locale.ENGLISH);

public static String dateTimeFromString(String s) {
    LocalDate date = LocalDate.parse(s);
    return date.toString(dateFormatter);
}

Let’s try it out:

    System.out.println(dateTimeFromString("2020-11-05"));

Output:

Thu Nov 05 2020

I have specified a locale for the formatter to make sure that the abbreviations printed are in English.

You cannot have a Date nor a LocalDate that prints as Thu Nov 05 2020. The toString methods of those classes are defined to print a specific format. You can have the format of your choice only in a String. And that’s good: it helps keeping your model and your presentation separate.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161