简体   繁体   中英

How to set the string to date dataType in java

I get date from the db, which contains date along with the timestamp, In the UI it displays both the date & timestanp, But I want to display only the date (yyyy-MM-dd). I have done the changes but i'm not able to set it right.

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String startDate = dateFormat.format(dataObj.getReportStartDate());
//The above startDate will be of the format : 2012-11-08

I want the startDate which is in the String format to be set into Date datatype.

dataIndexObj.setReportStartDate(startDate);
// Here the setReportStartDate is of the type date(), I cannot change its type to string.

I tried with parse(startDate) too.. It did not work. Any idea how to do it?

Your question isn't terribly clear, because you've already got the data as a Date - the value you originally formatted.

If you mean that you'll be getting the data back from the UI as a String , and need to convert that, you just need to use parse instead of format :

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
// TODO: Set the time zone as well...

Date date = dateFormat.parse(text);

maybe this is what you mean :

make 2 column in your db. first the type of string and the other date. so, when you need type of string you call a column of type string

Do not confuse java.util.Date and java.sql.Date . The first is poorly named and represents both a date and a time-of-day. The second is a bad hack of a class, extending from the first as a subclass, and pretending to hold only a date when in fact it does hold a time-of-day set to 00:00:00 UTC. Avoid these troublesome and poorly designed old classes. They are now legacy, supplanted by the java.time classes.

LocalDate

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

To get to such an object, convert your java.util.Date object by calling new methods added to the old date-time classes. We need an Instant along the way. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds .

Instant instant = myUtilDate.toInstant();

Perhaps you have a java.sql.Timestamp object from your database.

Instant instant = myTimestamp.toInstant();

Assign a time zone. A time zone is crucial because for any given moment the date varies around the globe by zone.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );

From that extract your LocalDate object.

LocalDate ld = zdt.toLocalDate();  // A date-only value, truncating time-of-day and forgetting time zone.

To generate a string in your desired format, merely call toString . That format is a standard ISO 8601 format. The java.time classes use ISO 8601 formats by default when parsing/generating String representations of date-time values.

String output = ld.toString();

Going the other direction.

LocalDate ld = LocalDate.parse( "2012-11-08" );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM