Justiciar
Justiciar

Reputation: 376

Java - Parsing a JLabel to java.util.Date

I'm simply trying to parse a string in JLabel to a date using a simpleDateFormatter(). Based On everything I've searched online, this code should work. However, I'm receiving the "cannot find symbol - method parse(java.lang.String)" error during compiliation. Any advice on how to resolve the issue would be greatly appreciated.

The JLabel in question was populated with a date from a database query using JDBC.

Additionally, I'm aware that that java.util.Date has been deprecated, but would still like to use it for this.

Code Snippet:

private Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
private JLabel dateDataLabel = new JLabel("");

private void setAndParseLabel()
{
    dateDataLabel.setText(formatter.format(validatePass.eventDate));

    java.util.Date aDate = formatter.parse(dateDataLabel.getText());
}

Upvotes: 0

Views: 142

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 340200

tl;dr

  • You are ignoring crucial issue of time zone. You are unwittingly parsing the input as a value in UTC.
  • You are using terrible old date-time classes that were supplanted years ago. Use java.time instead.

Example code:

LocalDateTime
.parse( 
    "2018-01-23 13:45".replace( " " , "T" )  // Comply with standard ISO 8601 format by replacing SPACE with `T`. Standard formats are used by default in java.time when parsing/generating strings.
)                                            // Returns a `LocalDateTime` object. This is *not* a moment, is *not* a point on the timeline.
.atZone(                                     // Apply a time zone to determine a moment, an actual point on the timeline.
    ZoneId.of( "America/Montreal" ) 
)                                            // Returns a `ZonedDateTime` object.
.toInstant()                                 // Adjust from a time zone to UTC, if need be.

java.time

The modern approach uses the java.time classes.

Your input string is almost in standard ISO 8601 format. To fully comply, replace that SPACE in the middle with a T.

String input = "2018-01-23 13:45".replace( " " , "T" ) ;

Parse as a LocalDateTime because your input has no indicator of time zone or offset-from-UTC.

LocalDateTime ldt = LocalDateTime.parse( input ) ;

A LocalDateTime by definition does not represent a moment, is not a point on the timeline. It represents potential moments along a range of about 26-27 hours (the range of time zones around the globe).

To determine a moment, assign a time zone (ZoneId) to get a ZonedDateTime object.

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( "Pacific/Auckland" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;

If you wish to see that same moment through the wall-clock time of UTC, extract an Instant.

Instant instant = zdt.toInstant() ;  // Adjust from some time zone to UTC.

Avoid java.util.Date where feasible. But if you must interoperate with old code not yet updated to java.time, you can convert back-and-forth. Call new conversion methods added to the old classes.

java.util.Date d = java.util.Date.from( instant ) ;  // Going the other direction: `myJavaUtilDate.toInstant()`

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?

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: 2

xingbin
xingbin

Reputation: 28289

java.text.Format does not have method parse, so the code does not compile.

You can refer it by java.text.DateFormat:

private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

Upvotes: 1

BackSlash
BackSlash

Reputation: 22243

There is no method parse in java.text.Format. Use java.text.DateFormat instead:

private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

Upvotes: 1

Related Questions