I have below dates,
24/04/2019
13/05/2019
12/04/2019
I want to remove the year and convert these dates into following format,
24/04
13/05
12/04
This not for output only I want to perform addition or apply for loop on that date.
First, pick the cells that contain dates, then right-click and select Format Cells. Select Custom in the Number Tab, then type 'dd-mmm-yyyy' in the Type text box, then click okay. It will format the dates you specify.
MonthDay // Represent a month & day-of-month, without a year.
.from( // Extract a `MonthDay` from a `LocalDate`, omitting the year but keeping the month and day-of-month.
LocalDate // Represent a date-only value, without time-of-day and without time zone.
.parse( // Parse a string to get a `LocalDate`.
"24/04/2019" , // FYI, better to use standard ISO 8601 formats rather than devising a custom format such as this.
DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) // Match the formatting pattern of your input strings.
) // Returns a `LocalDate`.
) // Returns a `MonthDay`.
.format( // Generate a `String` with text representing the value of this `MonthDay` object.
DateTimeFormatter.ofPattern( "dd/MM" )
) // Returns a `String`.
See this code run live at IdeOne.com.
24/04
LocalDate
& MonthDay
Java comes built-in with classes for this work:
LocalDate
MonthDay
Parse your input strings to get LocalDate
objects. Define a formatting pattern to match the input format.
String input = "24/04/2019" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
Parse.
LocalDate ld = LocalDate.parse( input , f ) ;
Generate a string in standard ISO 8601 format, YYYY-MM-DD. I strongly encourage you to use these formats when exchanging date-time values as text, rather than devising your own custom formats.
The java.time classes uses these standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.
String output = ld.toString() ;
2019-04-24
Extract just the month and day, while leaving out the year.
MonthDay md = MonthDay.from( ld ) ; // Extract the month & day-of-month, but omit the year.
Generate a string in standard ISO 8601 format. The format uses double hyphens in front to indicate the missing year.
String output = md.toString():
--04-24
You can parse this value as well.
MonthDay md = MonthDay.parse( "--04-24" ) ;
You can get back to a LocalDate
by specifying a year.
LocalDate ld = md.atYear( 2019 ) ; // Generate a `LocalDate` by specifying a year to go with the month & day.
not for output only I want to perform addition or apply for loop on that date.
Study the JavaDoc for both classes. Many handy methods are offered.
You can compare with isBefore
and isAfter
.
You can do math, adding or subtracting spans-of-time. You can adjust, such as jumping to a specific day-of-month. Note that the java.time classes follow the immutable objects pattern. So rather than changing (“mutating”) the original, a second object is created with values based on the original.
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With