This example demonstrate how to use the ISODateTimeFormat
class to format the date time information in Joda-Time.
package org.kodejava.joda;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
public class ISODateTimeFormatDemo {
public static void main(String[] args) {
DateTime dateTime = DateTime.now();
// Returns a basic formatter for a full date as four digit
// year, two-digit month of year, and two digit day of
// month yyyyMMdd.
System.out.println(dateTime.toString(
ISODateTimeFormat.basicDate()));
System.out.println(dateTime.toString(
ISODateTimeFormat.basicDateTime()));
System.out.println(dateTime.toString(
ISODateTimeFormat.basicDateTimeNoMillis()));
// Returns a formatter for a full ordinal date, using a four
// digit year and three digit dayOfYear yyyyDDD.
System.out.println(dateTime.toString(
ISODateTimeFormat.basicOrdinalDate()));
// Returns a basic formatter for a full date as four digit
// weekyear, two-digit week of weekyear, and one digit day
// of week xxxx'W'wwe
System.out.println(dateTime.toString(
ISODateTimeFormat.basicWeekDate()));
System.out.println(dateTime.toString(
ISODateTimeFormat.basicWeekDateTime()));
}
}
The result of the code above is printed below:
20211029
20211029T091631.884+0800
20211029T091631+0800
2021302
2021W435
2021W435T091631.884+0800
Maven Dependencies
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.7</version>
</dependency>
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
Hi,
I am trying to use JODA time to set format (MMddyyyy hh:mm a) , but it turns out it treats a 4 digit year as 2 digit. I get an error while parsing,
IllegalArgumentException
as 2013/9/8 formed as 13/9/8.What will be the solution to it?
Thanks!!
Hi,
Were you trying to convert a string to a
DateTime
? If yes, you can use theorg.joda.time.format.DateTimeFormatter
. Here is an example:Hi, In my spring with MongoDB project I want to fetch the data between two
ISODate
. How it is possible? Please help me. The data in the MongoDB is like following:Collection name is
mycollection
and there is a field name creationTime like this:and now I want to retrieve data from this collection on between two
logtime
by using Spring.