sande
sande

Reputation: 664

How to Store string data type value in date data type

I am trying to store string in date data type but I am not finding any solution

String string = "20171218";
try {
      DateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
      Date dateto=dateformat.parse(string); // convert stringDate to matching Date format
      String parsed = dateformat.format(dateto);
      System.out.println(parsed);

} catch (Exception e) {

}

On my above code my date is conversting again to string data type but, i want to store my date to Date data type

EX: Instead of String parsed = dateformat.format(dateto); I want to store as Date parsed = dateformat.format(dateto);, how to do work around for this

Upvotes: 0

Views: 874

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 340230

tl;dr

LocalDate.parse( 
    "20171218" , 
    DateTimeFormatter.BASIC_ISO_DATE 
) 

Avoid legacy date-time classes

You are using troublesome old date-time classes that are now legacy.

Also, you are inappropriately trying to represent a date-only value with a date + time-of-day type (java.util.Date).

Details

Use modern java.time classes.

The LocalDate class represents a date-only value without time-of-day and without time zone.

DateTimeFormatter f = DateTimeFormatter.BASIC_ISO_DATE ;  // Built-in, pre-defined. 
LocalDate ld = LocalDate.parse( "20171218" , f ) ;

Strings

Date-time objects do not have a “format”. Do not conflate strings representing the object’s value with the object itself. An object can generate a string to represent its value, and can parse a string to gain a value, but the object and string are distinct and separate.

Generate a string is standard ISO 8601 format by calling toString.

String output = ld.toString() ;

2017-12-18

To generate a string in the same format as your input, call format.

String output = ld.format( DateTimeFormatter.BASIC_ISO_DATE ) ;

20171218

The “basic” seen above refers to the type of ISO 8601 formats where the use of delimiters is minimized. I suggest you generally use the expanded formats where possible, in this case 2017-12-18. These expanded formats are used by default throughout the java.time classes when parsing/generating strings.

To generate strings in other formats, search Stack Overflow for info about DateTimeFormatter.

DateTimeParseException

To catch invalid inputs while parsing, trap for DateTimeParseException.

try {
    DateTimeFormatter f = DateTimeFormatter.BASIC_ISO_DATE ;
    LocalDate.parse( "20171218" , f ) ;
} catch ( DateTimeParseException e ) {
    … handle exception
}

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.

Upvotes: 4

Related Questions