Found 2616 Articles for Java

What are various ways to compare dates in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:49:47

358 Views

Using the LocalDate classThe java.time.LocalDate class represents the local date i.e. the date without time zone, you can use this object instead of Date. This class provides various methods such as isBefore(), isAfter() and, isEqual() to compare two dates −ExampleLive Demoimport java.time.LocalDate; public class Sample {    public static void main(String args[]) {         LocalDate date1 = LocalDate.of(2007, 11, 25);       LocalDate date2 = LocalDate.of(1999, 9, 12);             Boolean bool1 = date1.isAfter(date2);         Boolean bool2 = date1.isBefore(date2);       Boolean bool3 = date1.isEqual(date2);       if(bool1){ ... Read More

How to compare two dates in String format in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:49:19

6K+ Views

The java.text.SimpleDateFormat class is used to format and parse a string to date and date to string.One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object. To parse/convert a string as a Date object Instantiate this class by passing desired format string.Parse the date string using the parse() method.The util.Date class represents a specific instant time This class provides various methods such as before(), after() and, equals() to compare two datesExampleOnce you create date objects from strings you can compare them using either of these methods as shown below −Live Demoimport java.text.ParseException; ... Read More

What are various ways to compare time values in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:48:47

1K+ Views

The LocalTime class represents the local time i.e. the time without time zone. This class provides various methods such as isBefore(), isAfter() and, isEqual() to compare two times.ExampleLive Demoimport java.time.LocalTime; public class Test {    public static void main(String args[]) {         LocalTime Time1 = LocalTime.of(10, 15, 45);       LocalTime Time2 = LocalTime.of(07, 25, 55);             Boolean bool1 = Time1.isAfter(Time2);         Boolean bool2 = Time1.isBefore(Time2);       if(bool1){          System.out.println(Time1+" is after "+Time2);       }else if(bool2){          System.out.println(Time1+" ... Read More

How to compare two dates along with time in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:47:06

749 Views

The java.time.LocalDateTime class represents the local date and time i.e. the date without time zone, you can use this object instead of Date. This class provides various methods such as isBefore(), isAfter() and, isEqual() to compare two dates −ExampleLive Demoimport java.time.LocalDateTime; public class Test {    public static void main(String args[]) {         LocalDateTime dateTime1 = LocalDateTime.of(2007, 11, 25, 10, 15, 45);       LocalDateTime dateTime2 = LocalDateTime.of(1999, 9, 12, 07, 25, 55);             Boolean bool1 = dateTime1.isAfter(dateTime2);         Boolean bool2 = dateTime1.isBefore(dateTime2);       Boolean bool3 = ... Read More

How to Format a Date String using SimpleDateFormat in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:45:39

218 Views

One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat class. To parse/convert a string as a Date object −Instantiate this class by passing desired format string.Parse the date string using the parse() method.ExampleLive Demoimport java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Sample {    public static void main(String args[]) throws ParseException {         String date_string = "2007-25-06";       //Instantiating the SimpleDateFormat class       SimpleDateFormat formatter = new SimpleDateFormat("yyyy-dd-MM");             //Parsing the given String to Date object       Date ... Read More

How to create Date object from String value in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:45:19

2K+ Views

Using the SimpleDateFormat classOne of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat class. To parse/convert a string as a Date object −Instantiate this class by passing desired format string.Parse the date string using the parse() method.ExampleLive Demoimport java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Sample {    public static void main(String args[]) throws ParseException {         String date_string = "2007-25-06";       //Instantiating the SimpleDateFormat class       SimpleDateFormat formatter = new SimpleDateFormat("yyyy-dd-MM");             //Parsing the given String to Date object   ... Read More

How to get a Date from year, month and day in Java?

Maruthi Krishna
Updated on 05-Feb-2021 10:47:28

5K+ Views

Using the of() methodThe of() method of the java.time.LocalDate class accepts the values of the year, month, and day of month as parameters, creates and returns an object of the LocalDate.ExampleLive Demoimport java.time.LocalDate; public class Test {    public static void main(String[] args) {       LocalDate date = LocalDate.of(2014, 9, 11);       System.out.println("Date Value: "+date);    } }OutputDate Value: 2014-09-11Using the GregorianCalendar classOne of the constructors of the java.util.GregorianCalendar class accepts the values of year, month and day of month as values and creates a Calendar object representing it.Exampleimport java.util.*; class Test {      public static void main(String args[]){ ... Read More

How to get the current date in Java?

Maruthi Krishna
Updated on 06-Sep-2023 21:38:15

37K+ Views

You can get the current date in Java in various ways. Following are some of them −The constructor of Date classThe no-arg constructor of the java.util.Date class returns the Date object representing the current date and time, using this you can print the current date as shown below −ExampleLive Demoimport java.text.SimpleDateFormat; import java.text.ParseException; import java.util.Date; public class Demo {    public static void main(String args[])throws ParseException {             Date date = new Date();       SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy");        String str = formatter.format(date);       System.out.print("Current date: "+str);    } ... Read More

What is SimpleDateFormat in Java?

Maruthi Krishna
Updated on 05-Feb-2021 10:44:04

356 Views

The java.text.SimpleDateFormat class is used to format and parse a string to date and date to string.Parsing a date stringOne of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object. To parse/convert a string as a Date objectInstantiate this class by passing desired format string.Parse the date string using the parse() method.ExampleLive Demoimport java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Sample {    public static void main(String args[]) throws ParseException {         String date_string = "2007-25-06";       //Instantiating the SimpleDateFormat class       SimpleDateFormat formatter = ... Read More

How do I create a java.sql.Date object in Java?

Maruthi Krishna
Updated on 05-Feb-2021 10:41:48

5K+ Views

Using the ConstructorThe java.sql.Date represents the date value in JDBC. The constructor of this class accepts a long value representing the desired date and creates respective Date object.Date(long date)You can create this object using this constructor.ExampleLive Demoimport java.text.ParseException; import java.text.SimpleDateFormat; public class Demo {    public static void main(String args[]) throws ParseException {         String str = "26-09-1989";       SimpleDateFormat obj = new SimpleDateFormat("dd-MM-yyyy");             long epoch = obj.parse(str).getTime();             System.out.println("Date value: "+epoch);       //Creating java.util.Date object       java.util.Date date = ... Read More

Advertisements