What is java util date format

# Date Class

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 Date object and initializes it to represent time to the nearest millisecond. This date is used in the convert(java.util.Date utilDate) method to return a java.sql.Date object

public class UtilToSqlConversion  public static void main(String args[])  java.util.Date utilDate = new java.util.Date(); System.out.println("java.util.Date is : " + utilDate); java.sql.Date sqlDate = convert(utilDate); System.out.println("java.sql.Date is : " + sqlDate); DateFormat df = new SimpleDateFormat("dd/MM/YYYY - hh:mm:ss"); System.out.println("dateFormated date is : " + df.format(utilDate)); > private static java.sql.Date convert(java.util.Date uDate)  java.sql.Date sDate = new java.sql.Date(uDate.getTime()); return sDate; > > 
java.util.Date is : Fri Jul 22 14:40:35 IST 2016 java.sql.Date is : 2016-07-22 dateFormated date is : 22/07/2016 - 02:40:35 

java.util.Date has both date and time information, whereas java.sql.Date only has date information

# Java 8 LocalDate and LocalDateTime objects

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 information and not the time information.

Creates a LocalDate

// Create a default date LocalDate lDate = LocalDate.now(); // Creates a date from values lDate = LocalDate.of(2017, 12, 15); // create a date from string lDate = LocalDate.parse("2017-12-15"); // creates a date from zone LocalDate.now(ZoneId.systemDefault()); 

Creates a LocalDateTime

// Create a default date time LocalDateTime lDateTime = LocalDateTime.now(); // Creates a date time from values lDateTime = LocalDateTime.of(2017, 12, 15, 11, 30); // create a date time from string lDateTime = LocalDateTime.parse("2017-12-05T11:30:30"); // create a date time from zone LocalDateTime.now(ZoneId.systemDefault()); 

LocalDate to Date and vice-versa

Date date = Date.from(Instant.now()); ZoneId defaultZoneId = ZoneId.systemDefault(); // Date to LocalDate LocalDate localDate = date.toInstant().atZone(defaultZoneId).toLocalDate(); // LocalDate to Date Date.from(localDate.atStartOfDay(defaultZoneId).toInstant()); 

LocalDateTime to Date and vice-versa

Date date = Date.from(Instant.now()); ZoneId defaultZoneId = ZoneId.systemDefault(); // Date to LocalDateTime LocalDateTime localDateTime = date.toInstant().atZone(defaultZoneId).toLocalDateTime(); // LocalDateTime to Date Date out = Date.from(localDateTime.atZone(defaultZoneId).toInstant()); 

# Converting Date to a certain String format

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(dateFormat.format(today)); //25-Feb-16 

Patterns can be applied again by using applyPattern()

dateFormat.applyPattern("dd-MM-yyyy"); System.out.println(dateFormat.format(today)); //25-02-2016 dateFormat.applyPattern("dd-MM-yyyy HH:mm:ss E"); System.out.println(dateFormat.format(today)); //25-02-2016 06:14:33 Thu 

Note: Here mm (small letter m) denotes minutes and MM (capital M) denotes month. Pay careful attention when formatting years: capital «Y» ( Y ) indicates the «week in the year» while lower-case «y» ( y ) indicates the year.

# Creating a Specific Date

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 looking for the current date or have another Date instance already in hand.

To create a new date, you will need a Calendar instance. From there you can set the Calendar instance to the date that you need.

Calendar c = Calendar.getInstance(); 

This returns a new Calendar instance set to the current time. Calendar has many methods for mutating it’s date and time or setting it outright. In this case, we’ll set it to a specific date.

c.set(1974, 6, 2, 8, 0, 0); Date d = c.getTime(); 

The getTime method returns the Date instance that we need. Keep in mind that the Calendar set methods only set one or more fields, they do not set them all. That is, if you set the year, the other fields remain unchanged.

In many cases, this code snippet fulfills its purpose, but keep in mind that two important parts of the date/time are not defined.

  • the (1974, 6, 2, 8, 0, 0) parameters are interpreted within the default timezone, defined somewhere else,
  • the milliseconds are not set to zero, but filled from the system clock at the time the Calendar instance is created.

# Creating Date objects

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(); System.out.println(myBirthDate); // Mon Dec 31 00:00:00 IST 1990 

Date objects are best created through a Calendar instance since the use of the data constructors is deprecated and discouraged. To do se we need to get an instance of the Calendar class from the factory method. Then we can set year, month and day of month by using numbers or in case of months constants provided py the Calendar class to improve readability and reduce errors.

calendar.set(90, Calendar.DECEMBER, 11, 8, 32, 35); Date myBirthDatenTime = calendar.getTime(); System.out.println(myBirthDatenTime); // Mon Dec 31 08:32:35 IST 1990 

Along with date, we can also pass time in the order of hour, minutes and seconds.

# Comparing Date objects

# Calendar, Date, and LocalDate

# 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(); final Calendar calendar2 = Calendar.getInstance(); calendar2.set(1990, Calendar.NOVEMBER, 1, 0, 0, 0); Date samebirthdate = calendar2.getTime(); //Before example System.out.printf("Is %1$tF before %2$tF? %3$b%n", today, birthdate, Boolean.valueOf(today.before(birthdate))); System.out.printf("Is %1$tF before %1$tF? %3$b%n", today, today, Boolean.valueOf(today.before(today))); System.out.printf("Is %2$tF before %1$tF? %3$b%n", today, birthdate, Boolean.valueOf(birthdate.before(today))); //After example System.out.printf("Is %1$tF after %2$tF? %3$b%n", today, birthdate, Boolean.valueOf(today.after(birthdate))); System.out.printf("Is %1$tF after %1$tF? %3$b%n", today, birthdate, Boolean.valueOf(today.after(today))); System.out.printf("Is %2$tF after %1$tF? %3$b%n", today, birthdate, Boolean.valueOf(birthdate.after(today))); //Compare example System.out.printf("Compare %1$tF to %2$tF: %3$d%n", today, birthdate, Integer.valueOf(today.compareTo(birthdate))); System.out.printf("Compare %1$tF to %1$tF: %3$d%n", today, birthdate, Integer.valueOf(today.compareTo(today))); System.out.printf("Compare %2$tF to %1$tF: %3$d%n", today, birthdate, Integer.valueOf(birthdate.compareTo(today))); //Equal example System.out.printf("Is %1$tF equal to %2$tF? %3$b%n", today, birthdate, Boolean.valueOf(today.equals(birthdate))); System.out.printf("Is %1$tF equal to %2$tF? %3$b%n", birthdate, samebirthdate, Boolean.valueOf(birthdate.equals(samebirthdate))); System.out.printf( "Because birthdate.getTime() -> %1$d is different from samebirthdate.getTime() -> %2$d, there are millisecondes!%n", Long.valueOf(birthdate.getTime()), Long.valueOf(samebirthdate.getTime())); //Clear ms from calendars calendar.clear(Calendar.MILLISECOND); calendar2.clear(Calendar.MILLISECOND); birthdate = calendar.getTime(); samebirthdate = calendar2.getTime(); System.out.printf("Is %1$tF equal to %2$tF after clearing ms? %3$b%n", birthdate, samebirthdate, Boolean.valueOf(birthdate.equals(samebirthdate))); 

# isBefore, isAfter, compareTo and equals methods

//Use of LocalDate final LocalDate now = LocalDate.now(); final LocalDate birthdate2 = LocalDate.of(2012, 6, 30); final LocalDate birthdate3 = LocalDate.of(2012, 6, 30); //Hours, minutes, second and nanoOfsecond can also be configured with an other class LocalDateTime //LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond); //isBefore example System.out.printf("Is %1$tF before %2$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.isBefore(birthdate2))); System.out.printf("Is %1$tF before %1$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.isBefore(now))); System.out.printf("Is %2$tF before %1$tF? %3$b%n", now, birthdate2, Boolean.valueOf(birthdate2.isBefore(now))); //isAfter example System.out.printf("Is %1$tF after %2$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.isAfter(birthdate2))); System.out.printf("Is %1$tF after %1$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.isAfter(now))); System.out.printf("Is %2$tF after %1$tF? %3$b%n", now, birthdate2, Boolean.valueOf(birthdate2.isAfter(now))); //compareTo example System.out.printf("Compare %1$tF to %2$tF %3$d%n", now, birthdate2, Integer.valueOf(now.compareTo(birthdate2))); System.out.printf("Compare %1$tF to %1$tF %3$d%n", now, birthdate2, Integer.valueOf(now.compareTo(now))); System.out.printf("Compare %2$tF to %1$tF %3$d%n", now, birthdate2, Integer.valueOf(birthdate2.compareTo(now))); //equals example System.out.printf("Is %1$tF equal to %2$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.equals(birthdate2))); System.out.printf("Is %1$tF to %2$tF? %3$b%n", birthdate2, birthdate3, Boolean.valueOf(birthdate2.equals(birthdate3))); //isEqual example System.out.printf("Is %1$tF equal to %2$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.isEqual(birthdate2))); System.out.printf("Is %1$tF to %2$tF? %3$b%n", birthdate2, birthdate3, Boolean.valueOf(birthdate2.isEqual(birthdate3))); 

# Date comparison before Java 8

Before Java 8, dates could be compared using java.util.Calendar

(opens new window) classes. Date class offers 4 methods to compare dates :

Источник

Читайте также:  Python print all tuples
Оцените статью