0

I am getting the date in datetime with t as shown below

date1 = "2018-07-14T22:11"

I have to convert it into

"09 JUL 2018T22:11"

I have used

String contactDate = (new SimpleDateFormat("dd-MMM-yyyy HH")).format(new Date());

but it is without time. I want it with time in "09 JUL 2018T22:11" format.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Abhi
  • 41
  • 2
  • 9
  • 4
    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/). Also the modern `LocalDateTime` will parse your string without any explicit formatter. – Ole V.V. Jul 09 '18 at 06:53
  • 3
    I’m a bit surprised that your search and research and attempts didn’t at least bring you a bit closer. You may want to try a bit harder. Also is the month abbreviation required to be in uppercase? Not that it’s impossible. – Ole V.V. Jul 09 '18 at 06:55
  • 1
    If you are using Java 8+ you can use java.time like this `String date = "2018-07-14T22:11"; LocalDateTime ldt = LocalDateTime.parse(date); String newDate = ldt.format(DateTimeFormatter.ofPattern("dd MMM yyyy'T'HH:mm", Locale.US));` – Youcef LAIDANI Jul 09 '18 at 06:59
  • Just to be sure: You are getting July 14 and you want it converted into July 9? Or should it still be July 14, only in the second format given? – Ole V.V. Jul 09 '18 at 06:59
  • @YCF_L Thanks a lot. But is it necessary to include Locale.US? My form is html contact form on website which could be fill down by users around the world. – Abhi Jul 09 '18 at 09:49
  • In this case you have to search about [ZonedDateTime](https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html), beside I add `Locale.US` to show date in English format, for example I run in Frensh system, the date is in Frensh format.. – Youcef LAIDANI Jul 09 '18 at 09:53
  • If you are formatting your date-time for display to a user, you should probably format for that user’s locale. Assuming that the browser knows the locale, see for example [this question: Display date/time in user's locale format and time offset](https://stackoverflow.com/questions/85116/display-date-time-in-users-locale-format-and-time-offset). – Ole V.V. Jul 09 '18 at 10:10

3 Answers3

3

java.time

    DateTimeFormatter newFormatter
            = DateTimeFormatter.ofPattern("dd MMM uuuu'T'HH:mm", Locale.ENGLISH);
    String date1 = "2018-07-14T22:11";
    LocalDateTime dateTime = LocalDateTime.parse(date1);
    String contactDate = dateTime.format(newFormatter);
    System.out.println(contactDate);

(It’s pretty much what YCF_L already said in a comment.) Output:

14 Jul 2018T22:11

To get the month abbreviation in uppercase, like JUL: The straightforward and a bit hacky way is:

    String contactDate = dateTime.format(newFormatter).toUpperCase(Locale.ENGLISH);

With this change the output is:

14 JUL 2018T22:11

It only works because the string doesn’t contain any lowercase letters that we want to stay lowercase. To make sure only the month (and nothing else) is converted to uppercase:

    Map<Long, String> monthAbbreviations = Arrays.stream(Month.values())
            .collect(Collectors.toMap(m -> Long.valueOf(m.getValue()), 
                    m -> m.getDisplayName(TextStyle.SHORT, Locale.ENGLISH)
                            .toUpperCase(Locale.ENGLISH)));
    DateTimeFormatter newFormatter = new DateTimeFormatterBuilder()
            .appendPattern("dd ")
            .appendText(ChronoField.MONTH_OF_YEAR, monthAbbreviations)
            .appendPattern(" uuuu'T'HH:mm")
            .toFormatter(Locale.ENGLISH);

Now we get the desired result without calling toUpperCase on the entire result string.

If you need to subtract 5 days. I’m not sure you mean it, but you asked for July 9 as a result. Easy when you know how:

    dateTime = dateTime.minusDays(5);

With this line inserted into my first snippet above I get:

09 Jul 2018T22:11

What went wrong in your code

First, new Date() gives the current date and time and ignores the string you got with a future date. Second, your format pattern string contained only hours, not minutes. You need to use lowercase mm for minutes as I do in my code.

I do however recommend that you avoid the long outdated and notoriously troublesome SimpleDateFormat class. java.time, the modern Java date and time API, is so much nicer to work with.

Link

Oracle tutorial: Date Time explaining how to use java.time.

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

Hi in javascript it's easy just try

var d = new Date("2018-07-14T22:11"); console.log(d);

the log will be:

Sat Jul 14 2018 22:11:00 GMT+0530

you can refer date object in javascript here

Nakul Gawande
  • 456
  • 4
  • 4
0

In Java you can use:

public static void main(String[] args) {

    String datetime = DateTimeFormatter.ofPattern("dd MMM yyyy'T'HH:mm").format(LocalDateTime.now());
    System.out.println(datetime);
}

This will print on the console something like:

09 Jul 2018T08:07

If you want the month to be upper cased you can simply upper case the string:

DateTimeFormatter.ofPattern("dd MMM yyyy'T'HH:mm")
            .format(LocalDateTime.now()).toUpperCase();

You can also create a static method to perform this transformation:

public static String formatDateTime(LocalDateTime localDateTime) {
    Objects.requireNonNull(localDateTime, "Cannot format null date time");
    return DateTimeFormatter.ofPattern("dd MMM yyyy'T'HH:mm")
            .format(localDateTime).toUpperCase();
}
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76