CodeMed
CodeMed

Reputation: 9191

Get current date time in yyyy-MM-dd hh.mm.ss format

I have an application that will ALWAYS be run in only one time zone, so I do not need to worry about converting between time zones. However, the date-time must always be printed out in the following format:

yyyy-MM-dd hh.mm.ss 

The code below fails to print the proper format:

public void setCreated(){
    DateTime now = new org.joda.time.DateTime();
    String pattern = "yyyy-MM-dd hh.mm.ss";
    created  = DateTime.parse(now.toString(), DateTimeFormat.forPattern(pattern));
    System.out.println("''''''''''''''''''''''''''' created is: "+created);
}  

The setCreated() method results in the following output:

"2013-12-16T20:06:18.672-08:00"

How can I change the code in setCreated() so that it prints out the following instead?

"2013-12-16 20:06:18"

Upvotes: 5

Views: 35163

Answers (6)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 78995

java.time

Shown below is a notice on the Joda-Time Home Page:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern date-time API in Java

ZonedDateTime.now(ZoneId.systemDefault())
             .format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"))

Some important points about this solution:

  1. Replace ZoneId.systemDefault() with the applicable ZoneId e.g. ZoneId.of("America/New_York").
  2. If the current date-time is required in the JVM's default time zone, you do not need to use ZonedDateTime#now(ZoneId); instead, you can use ZonedDateTime#now().
  3. For the current date-time, you can also use LocalDateTime instead of ZonedDateTime e.g. LocalDateTime#now(ZoneId) or LocalDateTime#now(); but ZonedDateTime makes it easier to convert it from one time zone to another or get the date-time with time zone offset etc.
  4. You can use y instead of u in the pattern for DateTimeFormatter but I prefer u to y.
  5. The symbol h is used for clock-hour-of-am-pm (1-12) and makes sense only with the am/pm marker for which you have to use a in the pattern. For hour-of-day (0-23), you have to use the symbol H. Check documentation to learn more about these symbols.

Demo:

class Main {
    public static void main(String args[]) {
        // Replace ZoneId.systemDefault() with the applicable ZoneId e.g.
        // ZoneId.of("America/New_York")
        ZonedDateTime zdt = ZonedDateTime.now(ZoneId.systemDefault());
        String formattedDateTimeStr = zdt.format(
                DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH));
        System.out.println(formattedDateTimeStr);
    }
}

Output from a sample run:

2024-12-03 19:56:18

Online Demo

Learn more about the modern Date-Time API from Trail: Date Time.

Upvotes: 1

totran
totran

Reputation: 89

And now in Java 9, you can use this:

LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh.mm.ss"));

Upvotes: 2

rocks
rocks

Reputation: 49

Try this:

org.joda.time.DateTime now = new org.joda.time.DateTime();
String pattern = "yyyy-MM-dd hh.mm.ss";
DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern);
String formatted = formatter.print(now);
LocalDateTime date = formatter.parseLocalDateTime(formatted);
System.out.println(date.toDateTime());

Upvotes: 1

AmirtharajCVijay
AmirtharajCVijay

Reputation: 1108

try this

        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
        Date now = new Date();
        String strDate = sdfDate.format(now);
        System.out.println(strDate);

demo

Upvotes: 2

Lijo
Lijo

Reputation: 6778

 public static void main(String args[])
{

SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//dd/MM/yyyy
Date now = new Date();
String strDate = sdfDate.format(now);
    System.out.println(strDate);
}

out put 2013-12-17 09:48:11

Upvotes: 4

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279890

You aren't parsing anything, you are formatting it. You need to use DateTimeFormatter#print(ReadableInstant).

DateTime now = new org.joda.time.DateTime();
String pattern = "yyyy-MM-dd hh.mm.ss";
DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern);
String formatted = formatter.print(now);
System.out.println(formatted);

which prints

2013-12-16 11.13.24

This doesn't match your format, but I'm basing it on your code, not on your expected output.

Upvotes: 10

Related Questions