'How to Remove year(yyyy) in date(dd/mm/yyyy)

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.



Solution 1:[1]

You can get the substring from index 0 to 4:

String input = "24/04/2019";
String output = input.substring(0, 5);

Solution 2:[2]

tl;dr

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:

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.


About java.time

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.

Solution 3:[3]

Use this:

String input = "24/04/2019";
String output = input.substring(0,input.lastIndexOf("/"));

Solution 4:[4]

You may also try with string split logic as below.

String date="24/04/2019";

String [ ] ddyymm=date.split("/");

 String output=ddyymm[0]+"/"+ddmmyy[1];

Solution 5:[5]

I'm using the following code to cover all cases where the year may come at the begginning

var dateFormat = deviceDateFormat.replace("(?i)y".toRegex(), "")
if (!dateFormat.first().isLetter()) dateFormat = dateFormat.drop(1)
if (!dateFormat[dateFormat.length - 1].isLetter()) dateFormat = dateFormat.dropLast(1)
println(dateFormat)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Majid
Solution 4 Ramesh
Solution 5 3ameration