0

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

azro
  • 53,056
  • 7
  • 34
  • 70
sande
  • 567
  • 1
  • 10
  • 24
  • 6
    A blank `catch` block is never a good idea. If there is an exception, it will silently ignore it and you won't know that an exception occurred. – rgettman Dec 18 '17 at 22:20
  • Thanks, I will add a print statement but my Main concern is regarding conversion. – sande Dec 18 '17 at 22:22
  • 3
    But you've already shown the code to convert it. What on earth are you asking? – Dawood ibn Kareem Dec 18 '17 at 22:23
  • I parsed to String data type String parsed = dateformat.format(dateto); I want it in Date data type – sande Dec 18 '17 at 22:25
  • 2
    `dateto` **is** the date. – PM 77-1 Dec 18 '17 at 22:25
  • I am getting date as Mon Dec 18 00:00:00 PST 2017 but I want date as 20171218 – sande Dec 18 '17 at 22:27
  • 1
    Getting **where**? `Date` object can have numerous text representations. The value stays the same. – PM 77-1 Dec 18 '17 at 22:31
  • When I am trying to print dateto – sande Dec 18 '17 at 22:32
  • 1
    So you want a Date? Or you want the String `"20171218"`? Which is it? – Dawood ibn Kareem Dec 18 '17 at 22:44
  • I want dateto in yyyyMMdd format – sande Dec 18 '17 at 22:45
  • 1
    @sande Core concept: **A date-time object such as `java.util.Date` does *not* have a “format”.** Only strings have a format. And please search Stack Overflow before posting. This has been explained many many many times already. – Basil Bourque Dec 18 '17 at 22:49
  • @sande, it sounds like you're saying you want a `String` - the `String` `"20171218"`. Please remember that a `Date` is _just_ a moment in time - nothing else. It doesn't have numbers or characters inside. It doesn't have a timezone. It doesn't have a format. So you _can't_ have a `Date` with a `yyyyMMdd` format. That simply doesn't exist. You can have a `Date`, or you can have a `String` with a value like `"20171218"`. They are two entirely different things. – Dawood ibn Kareem Dec 19 '17 at 02:10
  • Having said all that, if you ask Java to print a `Date`, and you don't convert it to a `String` first, you'll get something printed - but it's the result of the internals of Java applying a guess as to what you want to see. That's when you get something that looks like `"Mon Dec 18 00:00:00 PST 2017"`. That doesn't mean that those characters are part of the `Date` - they're not. They're simply how Java chooses to display dates, if you don't specify a different way to have them displayed. – Dawood ibn Kareem Dec 19 '17 at 02:13

1 Answers1

4

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154