Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 583
John dahat Avatar asked Jan 26 '19 04:01

John dahat


People also ask

How do I change date format from DD MM to YYYY?

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.


1 Answers

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:

  • LocalDate
    Represents a date-only value without time-of-day and without time zone or offset-from-UTC.
  • MonthDay
    Represents a day in a month, generically speaking, without a year.

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?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

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.

like image 126
Basil Bourque Avatar answered Sep 21 '22 08:09

Basil Bourque