Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to parse a date in YYYY-MM-DD format to YYYYMMDD. If I use the following function, it returns me a YYYYMMDD format but with a different DD. i.E: 2013-05-16 BECOMES 20130515

Apologies for sounding illiterate :) I am new to Java.

Any help would be appreciated.

String TestDate=yyyymmddParser.format(oLifEExtension.TestDate().getTime());
                    sb.append(TestDate)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
80 views
Welcome To Ask or Share your Answers For Others

1 Answer

Not sure if the Question was meant for java.util.Date (a date plus time-of-day) or a java.sql.Date (a date-only). In both cases, you should be using the modern java.time classes rather than the troublesome legacy date-time classes.

Some other java.sql.Date questions are linked as duplicates of this one. So I handle both classes (sql & util) here.

java.util.Date

The legacy java.util.Date class represents a moment on the timeline in UTC. This means a date with a time-of-day. The trick is that your input string is for a date-only value. You can first parse your string as a date-only value, then assign a time-of-day if desired.

Your input string complies with the standard ISO 8601 format of YYYY-MM-DD. The java.time classes default to the standard formats when parsing/generating strings. So no need to specify a formatting pattern.

LocalDate ld = LocalDate.parse( "2013-05-16" ) ;

For a time-of-day, you probably want the first moment of the day. Do not assume the first moment is 00:00:00. In some time zones an anomaly such as Daylight Saving Time (DST) may cause a day to start at a different time such as 01:00:00. To account for such anomalies, we must specify a time zone in determining the first moment of the day.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;

Apply that zone in asking java.time to determine the first moment of the day. We produce a ZonedDateTime object as the result.

ZonedDateTime zdt = ld.atStartOfDay( z ) ;

If you desire a specific time of day, apply a LocalTime object. Keep in mind that your time-of-day may not be valid on that particular date for that particular zone. For example, you may be specifying a time-of-day occurring during a DST cutover. In such a case, the ZonedDateTime class has a policy for adjusting to accommodate. Be sure to read the documentation to understand that policy and the resulting behavior.

LocalTime lt = LocalTime.of( 12 , 0 ) ; 
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;  // Time-of-day may be adjusted as needed.

java.sql.Date

No need to use java.sql.Date. That class is replaced by LocalDate. The LocalDate class represents a date-only value without time-of-day and without time zone.

JDBC drivers that comply with JDBC 4.2 can deal directly with java.time types by calling:

For presentation of the LocalDate to the user, generate a String for display in your user-interface. Use a DateTimeFormatter to automatically localize. To localize, specify:

  • FormatStyle to determine how long or abbreviated should the string be.
  • Locale to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

Example:

Locale l = Locale.CANADA_FRENCH ; 
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM ).withLocale( l );
String output = zdt.format( f );

You can go the other direction as well, parsing an input string to get a date.

LocalDate ld = LocalDate.parse( input , f ) ;

Trap for the exception thrown if the user’s input is faulty or unexpected.

try{ 
    LocalDate ld = LocalDate.parse( input , f ) ;
    myPrepStmt.setObject( … , ld ) ;
} catch ( DateTimeParseException e ) {
    … // Handle the error condition of faulty/unexpected input by user.
}

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...