The other Answers are correct about you not including proper class structure with main method.
Never use Date
Also, never use the Date
class. It is terribly designed, and quite confusing and troublesome. Now legacy, supplanted by the java.time.Instant
class.
java.time
To represent a moment in UTC, use Instant
. That class uses a resolution as fine as nanoseconds. The current moment may be captured with only milliseconds or microseconds depending on the limitations of your host computer clock hardware, your host operating system, and your Java Virtual Machine implementation.
Example code.
package com.basilbourque.example.telltimeapp ;
import java.time.Instant ;
public class TellTime {
public static void main( String args[] )
{
Instant instant = Instant.now() ; // Capture the current moment in UTC.
String output = instant.toString() ; // Generate text in a `String` object representing the value of the `Instant` object in standard ISO 8601 format.
System.out.println( output ) ; // Dump text to the console.
}
}
Lets get more realistic. Launch an instance of our app in the main
method. Then invoke a method on that app object to get the ball rolling.
Our app knows how to do two things:
Our main
method asks for the first of those two.
package com.basilbourque.example.telltimeapp ;
import java.time.Instant ;
public class TellTime {
public static void main( String args[] ) // The `main` method is *not* object-oriented, just a hack, the way we solve the chicken-or-the-egg dilemma] of how to launch an OOP app.
{
TellTime app = new TellTime() ; // Create an instance of our app, an object that represents the entire running app.
app.tellCurrentMomentInUtc() ; // Ask our app object to run some code.
}
public void tellCurrentMomentInUtc()
{
Instant instant = Instant.now() ; // Capture the current moment in UTC.
String output = instant.toString() ; // Generate text in a `String` object representing the value of the `Instant` object in standard ISO 8601 format.
System.out.println( output ) ; // Dump text to the console. 2018-07-02T20:17:49.929677Z
}
public void tellCurrentMomentInMontreal()
{
Instant instant = Instant.now() ; // Capture the current moment in UTC.
ZoneId z = ZoneId.of( "America/Montreal" ) ; // A time zone is a history of the past, present, and future changes to the offset-from-UTC used by the people of a particular region.
ZonedDateTime zdt = instant.atZone( z ) ; // Adjust from UTC to Québec time. Same moment, same point on the timeline, different wall-clock time.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( Locale.CANADA_FRENCH ) ; // Be aware that time zone and locale have *nothing* to do with one another, completely orthogonal issues.
String output = zdt.format( f ) ; // Generate text in a localized format.
System.out.println( output ) ; // Dump text to the console. Example: lundi 2 juillet 2018 à 16:16:59 Eastern Daylight Time
}
}
Tutorials
Study the Java Tutorials by Oracle before posting to Stack Overflow.
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.