Tutorial by Examples

Date date = new Date(); System.out.println(date); // Thu Feb 25 05:03:59 IST 2016 Here this Date object contains the current date and time when this object was created. Calendar calendar = Calendar.getInstance(); calendar.set(90, Calendar.DECEMBER, 11); Date myBirthDate = calendar.getTime(); ...
Calendar, Date, and LocalDate Java SE 8 before, after, compareTo and equals methods //Use of Calendar and Date objects final Date today = new Date(); final Calendar calendar = Calendar.getInstance(); calendar.set(1990, Calendar.NOVEMBER, 1, 0, 0, 0); Date birthdate = calendar.getTime(); ...
format() from SimpleDateFormat class helps to convert a Date object into certain format String object by using the supplied pattern string. Date today = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy"); //pattern is specified here System.out.println(date...
parse() from SimpleDateFormat class helps to convert a String pattern into a Date object. DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US); String dateStr = "02/25/2016"; // input String Date date = dateFormat.parse(dateStr); System.out.println(date.getYe...
Using the following code with the format string yyyy/MM/dd hh:mm.ss, we will receive the following output 2016/04/19 11:45.36 // define the format to use String formatString = "yyyy/MM/dd hh:mm.ss"; // get a current date object Date date = Calendar.getInstance().getTime(); //...
This method can be used to convert a formatted string representation of a date into a Date object. /** * Parses the date using the given format. * * @param formattedDate the formatted date string * @param dateFormat the date format which was used to create the string. ...
While the Java Date class has several constructors, you'll notice that most are deprecated. The only acceptable way of creating a Date instance directly is either by using the empty constructor or passing in a long (number of milliseconds since standard base time). Neither are handy unless you're lo...
Date and LocalDate objects cannot be exactly converted between each other since a Date object represents both a specific day and time, while a LocalDate object does not contain time or timezone information. However, it can be useful to convert between the two if you only care about the actual date i...
A java.util.Date object does not have a concept of time zone. There is no way to set a timezone for a Date There is no way to change the timezone of a Date object A Date object created with the new Date() default constructor will be initialised with the current time in the system default timezo...
java.util.Date to java.sql.Date conversion is usually necessary when a Date object needs to be written in a database. java.sql.Date is a wrapper around millisecond value and is used by JDBC to identify an SQL DATE type In the below example, we use the java.util.Date() constructor, that creates a D...
To use just the time part of a Date use LocalTime. You can instantiate a LocalTime object in a couple ways LocalTime time = LocalTime.now(); time = LocalTime.MIDNIGHT; time = LocalTime.NOON; time = LocalTime.of(12, 12, 45); LocalTime also has a built in toString method that displays the for...

Page 1 of 1