Reputation: 9191
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
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.
java.time
, the modern date-time API in JavaZonedDateTime.now(ZoneId.systemDefault())
.format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"))
Some important points about this solution:
ZoneId.systemDefault()
with the applicable ZoneId
e.g. ZoneId.of("America/New_York")
.ZonedDateTime#now(ZoneId)
; instead, you can use ZonedDateTime#now()
.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.y
instead of u
in the pattern for DateTimeFormatter
but I prefer u
to y
.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
Learn more about the modern Date-Time API from Trail: Date Time.
Upvotes: 1
Reputation: 89
And now in Java 9, you can use this:
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh.mm.ss"));
Upvotes: 2
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
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);
Upvotes: 2
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
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