I have a spinner called Clock which holds time. But problem is that I need all data separately since that data (month, day... will be used for comparison). I have this code, this return huge strange line of string (Thu May 11 18:02:00 EEST 2017) which I cant even split and store into array (just prints whole line) nor write to file (just empty file). Is there is any way to get month, day, year from this JSpinner??
private void ClockStateChanged(javax.swing.event.ChangeEvent evt) {
Date date = (Date) Clock.getValue();
System.out.print(date);
String dateString = null;
SimpleDateFormat sdfr = new SimpleDateFormat("");
dateString = sdfr.format(date);
System.out.println( dateString );
}
myJavaUtilDate.toInstant()
.atZone( ZoneId.of( "America/Montreal" ) )
.toLocalDate()
.toString()
2017-01-23
Do not confuse a date-time object with a String representing its value. A date-time object can be created by parsing a string. And a date-time object can generate a string to represent its value. But the string and the date-time object are distinct and separate. So a date-time object does not “have a format”.
You are using the troublesome old date-time classes (Date
& Calendar
) that are now legacy, supplanted by the java.time classes.
If source of data (Swing) has not been updated to handle java.time types directly, convert. Look to new conversion methods added to the old classes.
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Instant instant = myJavaUtilDate.toInstant() ;
instant.toString(): 2017-01-23T06:23:45.678Z
Both java.util.Date
and Instant
are in UTC. To determine a date, you must assign a time zone. For any given moment the date varies around the world by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
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" ) ; // Or ZoneId.systemDefault()
ZonedDateTime zdt = instant.atZone( z );
zdt.toString(): 2017-01-23T01:23:45.678+05:00[America/Montreal]
Now you can extract the date-only portion. The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate ld = zdt.toLocalDate();
Your Question is not clear about your goal. If you just want to serialize this date value for later comparison, write as text in standard ISO 8601 format. The java.time classes use ISO 8601 formats by default when parsing/generating strings.
I suggest you stick to the standard ISO 8601 formats for strings whenever possible. They are designed to minimize ambiguity. They are easy to parse by machine. They are easy to read by humans across various cultures.
String output = ld.toString() ;
2017-01-23
If you do want the individual parts of year, month, and dayOfMonth, interrogate the LocalDate
object. Unlike the legacy classes, in java.time the months have sane numbering with 1-12 for January-December.
int year = ld.getYear() ;
int month = ld.getMonthValue() ;
int dayOfMonth = ld.getDayOfMonth() ;
You can also get the month as a powerful Month
enum object.
Month m = ld.getMonth();
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.