hereral/Clara-Training-Data huggingface dataset:
Question: Can you provide an example of Java YearMonth: get()?
 
Answer: import java.time.YearMonth;  
import java.time.temporal.ChronoField;  
public class YearMonthExample3 {  
  public static void main(String[] args) {  
    YearMonth y = YearMonth.now();  
    long l1 = y.get(ChronoField.YEAR);  
    System.out.println(l1);  
    long l2 = y.get(ChronoField.MONTH_OF_YEAR);  
    System.out.println(l2);  
  }  
}  
Output: 2017
             1
Question: Can you provide an example of Java YearMonth: plus()?

Answer: import java.time.*;  
public class YearMonthExample4 {  
  public static void main(String[] args) {  
    YearMonth ym1 = YearMonth.now();  
    YearMonth ym2 = ym1.plus(Period.ofYears(2));  
    System.out.println(ym2);  
  }  
}  
Output: 2019-01
Question: Can you provide an example of Java YearMonth: minus()?

Answer: import java.time.*;  
public class YearMonthExample5 {  
  public static void main(String[] args) {  
    YearMonth ym1 = YearMonth.now();  
    YearMonth ym2 = ym1.minus(Period.ofYears(2));  
    System.out.println(ym2);  
  }  
}  
Output: 2015-01
Question: What is Java Period class?

Answer: Java Period class is used to measures time in years, months and days. It inherits the Object class and implements the ChronoPeriod interface.
Question: What is the class declaration for Java Period class?

Answer: public final class Period extends Object implements ChronoPeriod, Serializable
Question: What are the Methods of Java Period?

Answer: Methods of Java Period

Method:	Temporal addTo(Temporal temporal)	
Description: It is used to add this period to the specified temporal object.

Method:	long get(TemporalUnit unit)	
Description: It is used to get the value of the requested unit.

Method:	int getYears()	
Description: It is used to get the amount of years of this period.

Method:	boolean isZero()	
Description: It is used to check if all three units of this period are zero.

Method:	Period minus(TemporalAmount amountToSubtract)	
Description: It is used to return a copy of this period with the specified period subtracted.

Method:	static Period of(int years, int months, int days)	
Description: It is used to obtain a Period representing a number of years, months and days.

Method:	Period plus(TemporalAmount amountToAdd)	
Description: It is used to return a copy of this period with the specified period added.

Method:	static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)	
Description: It obtains a Period consisting of the number of years, months, and days between two dates.

Method:	boolean equals(Object obj)	
Description: It checks if this period is equal to another period.

Method:	IsoChronology	getChronology() 
Description: It gets the chronology of this period, which is the ISO calendar system.

Method:	int getDays()	
Description: It gets the amount of days of this period.

Method:	int getMonths()	
Description: It gets the amount of months of this period.

Method:	List getUnits()	
Description: It gets the set of units supported by this period.

Method:	Period multipliedBy(int scalar)	
Description: It returns a new instance with each element in this period multiplied by the specified scalar.

Method:	Period normalized()	
Description: It returns a copy of this period with the years and months normalized.

Method:	Period plusDays(long daysToAdd)	
Description: It returns a copy of this period with the specified days added.

Method:	Period plusMonths(long monthsToAdd)	
Description: It returns a copy of this period with the specified months added.

Method:	Period plusYears(long yearsToAdd)	
Description: It returns a copy of this period with the specified years added.

Method:	Temporal subtractFrom(Temporal temporal)	
Description: It subtracts this period from the specified temporal object.

Method:	Period negated()	
Description: It returns a new instance with each amount in this period negated.
Question: Can you provide an example of Java Period: addTo()?

Answer: PeriodExample1.java

import java.time.*;  
import java.time.temporal.Temporal;  
public class PeriodExample1 {  
  public static void main(String[] args) {  
    Period period = Period.ofDays(24);  
    Temporal temp = period.addTo(LocalDate.now());  
    System.out.println(temp);  
  }  
}  

Output:
2017-02-24
Question: Can you provide an example of Java Period: of()?

Answer: PeriodExample2.java

import java.time.Period;  
public class PeriodExample2 {  
public static void main(String[] args) {  
  Period period = Period.of(2017,02,16);  
  System.out.println(period.toString());  
}  
}  

Output:
P2017Y2M16D
Question: Can you provide an example of Java Period: minus()?

Answer: PeriodExample3.java

import java.time.Period;  
public class PeriodExample3 {  
  public static void main(String[] args) {  
    Period period1 = Period.ofMonths(4);   
    Period period2 = period1.minus(Period.ofMonths(2));  
    System.out.println(period2);  
  }  
}  

Output:
P2M
Question: Can you provide an example of Java Period: plus()?

Answer: PeriodExample4.java

import java.time.Period;  
public class PeriodExample4 {  
  public static void main(String[] args) {  
    Period period1 = Period.ofMonths(4);   
    Period period2 = period1.plus(Period.ofMonths(2));  
    System.out.println(period2);  
  }  
}  

Output:
P6M
Question: What is Java Duration class?

Answer: Java Duration class is used to measures time in seconds and nanoseconds. It inherits the Object class and implements the Comparable interface.
Question: What is the class declaration for Java Duration class?

Answer: Let's see the declaration of java.time.Duration class.

public final class Duration extends Object   
implements TemporalAmount, Comparable<Duration>, Serializable
Question: What are the Methods of Java Duration?

Answer: Methods of Java Duration

Method:	Temporal addTo(Temporal temporal)	
Description: It is used to add this duration to the specified temporal object.

Method:	static Duration between(Temporal startInclusive, Temporal endExclusive)	
Description: It is used to obtain a Duration representing the duration between two temporal objects.

Method:	long get(TemporalUnit unit)	
Description: It is used to get the value of the requested unit.

Method:	boolean isNegative()	
Description: It is used to check if this duration is negative, excluding zero.

Method:	boolean isZero()	
Description: It is used to check if this duration is zero length.

Method:	Duration minus(Duration duration)	
Description: It is used to return a copy of this duration with the specified duration subtracted.

Method:	Duration plus(Duration duration)	
Description: It is used to return a copy of this duration with the specified duration added.

Method:	Duration abs()	
Description: It returns a copy of this duration with a positive length.

Method:	static Duration between(Temporal startInclusive, Temporal endExclusive)	
Description: It obtains a Duration representing the duration between two temporal objects.

Method:	int compareTo(Duration otherDuration)	
Description: It compares the given duration to the specified Duration.

Method:	int getNano()	
Description: It gets the number of nanoseconds within the second in this duration.

Method:	long getSeconds()	
Description: It gets the number of seconds in this duration.

Method:	static Duration of(long amount, TemporalUnit unit)	
Description: It obtains a Duration representing an amount in the specified unit.

Method:	static Duration ofDays(long days)	
Description: It obtains a Duration representing a number of standard 24 hour days.

Method:	static Duration ofHours(long hours)	
Description: It obtains a Duration representing a number of standard hours.

Method:	static Duration ofMillis(long millis)	
Description: It obtains a Duration representing a number of milliseconds.

Method:	static Duration ofMinutes(long minutes)	
Description: It obtains a Duration representing a number of standard minutes.

Method:	static Duration ofNanos(long nanos)	
Description: It obtains a Duration representing a number of nanoseconds.

Method:	static Duration ofSeconds(long seconds)	
Description: It obtains a Duration representing a number of seconds.
Question: Can you provide an example of Java Duration: get()?

Answer: DurationExample1.java

import java.time.*;  
import java.time.temporal.ChronoUnit;  
public class DurationExample1 {  
  public static void main(String[] args) {  
    Duration d = Duration.between(LocalTime.NOON,LocalTime.MAX);  
    System.out.println(d.get(ChronoUnit.SECONDS));    
  }  
}  

Output:
43199
Question: Can you provide an example of Java Duration: isNegative()?

Answer: DurationExample2.java

import java.time.*;  
public class DurationExample2 {  
  public static void main(String[] args) {  
      Duration d1 = Duration.between(LocalTime.MAX,LocalTime.NOON);  
      System.out.println(d1.isNegative());  
      Duration d2 = Duration.between(LocalTime.NOON,LocalTime.MAX);  
              System.out.println(d2.isNegative());      
  }  
}  

Output:
true
false
Question: Can you provide an example of Java Duration: between()?

Answer: DurationExample3.java

import java.time.*;  
import java.time.temporal.ChronoUnit;  
public class DurationExample3 {  
  public static void main(String[] args) {  
    Duration d = Duration.between(LocalTime.NOON,LocalTime.MAX);  
    System.out.println(d.get(ChronoUnit.SECONDS));    
  }  
}  

Output:
43199
Question: Can you provide an example of Java Duration: minus()?

Answer: DurationExample4.java

import java.time.*;  
public class DurationExample4 {  
  public static void main(String[] args) {  
    Duration d1 = Duration.between(LocalTime.NOON,LocalTime.MAX);  
    System.out.println(d1.getSeconds());  
    Duration d2 = d1.minus(d1);  
    System.out.println(d2.getSeconds());  
  }  
}  

Output:
43199
0
Question: Can you provide an example of Java Duration: plus()?

Answer: DurationExample5.java

import java.time.*;  
public class DurationExample5 {  
  public static void main(String[] args) {  
    Duration d1 = Duration.between(LocalTime.NOON,LocalTime.MAX);  
    System.out.println(d1.getSeconds());  
    Duration d2 = d1.plus(d1);  
    System.out.println(d2.getSeconds());  
  }  
}  

Output:
43199
86399
Question: What is Java Instant Class?

Answer: DurationExample5.java

import java.time.*;  
public class DurationExample5 {  
  public static void main(String[] args) {  
    Duration d1 = Duration.between(LocalTime.NOON,LocalTime.MAX);  
    System.out.println(d1.getSeconds());  
    Duration d2 = d1.plus(d1);  
    System.out.println(d2.getSeconds());  
  }  
}  

Output:
43199
86399
Question: What is the class declaration for Java Instant class?

Answer: Let's see the declaration of java.time.Instant class.

public final class Instant extends Object   
implements Temporal, TemporalAdjuster, Comparable<Instant>, Serializable
Question: Can you provide an example of Java Instant: parse()?

Answer: InstantExample1.java

import java.time.Instant;  
public class InstantExample1 {  
  public static void main(String[] args) {  
    Instant inst = Instant.parse("2017-02-03T10:37:30.00Z");  
    System.out.println(inst);  
  }  
}  

Output:
2017-02-03T10:37:30Z
Question: Can you provide an example of Java Instant: now()?

Answer: InstantExample2.java

import java.time.Instant;  
public class InstantExample2 {  
  public static void main(String[] args) {  
    Instant instant = Instant.now();  
    System.out.println(instant);    
  }  
}  

Output:
2017-02-03T06:11:01.194Z
Question: Can you provide an example of Java Instant: minus()?

Answer: InstantExample3.java

import java.time.*;  
public class InstantExample3 {  
  public static void main(String[] args) {  
    Instant instant = Instant.parse("2017-02-03T11:25:30.00Z");  
    instant = instant.minus(Duration.ofDays(125));  
    System.out.println(instant);   
  }  
}  

Output:
2016-10-01T11:25:30Z
Question: Can you provide an example of Java Instant: plus()?

Answer: InstantExample4.java

import java.time.*;  
public class InstantExample4 {  
  public static void main(String[] args) {  
    Instant inst1 = Instant.parse("2017-02-03T11:25:30.00Z");  
    Instant inst2 = inst1.plus(Duration.ofDays(125));  
    System.out.println(inst2);   
  }  
}  

Output:
2017-06-08T11:25:30Z
Question: Can you provide an example of Java Instant: isSupported()?

Answer: InstantExample5.java

import java.time.Instant;  
import java.time.temporal.ChronoUnit;  
public class InstantExample5 {  
  public static void main(String[] args) {  
    Instant inst = Instant.parse("2017-02-03T11:35:30.00Z");  
    System.out.println(inst.isSupported(ChronoUnit.DAYS));  
    System.out.println(inst.isSupported(ChronoUnit.YEARS));      
  }  
}  

Output:
true
false
Question: What is Java DayOfWeek enum?

Answer: In Java the DayOfWeek is an enum representing the 7 days of the week. In addition with the textual enum name, every day-of-week has an int value.
Question: What is the class declaration for Java DayOfWeek enum?

Answer: Let's see the declaration of java.time.DayOfWeek.

public enum DayOfWeek extends Enum<DayOfWeek> implements TemporalAccessor, TemporalAdjuster
Question: What are Enum constants?

Answer: Enum Constants
        
Constants: SUNDAY        
Description: The singleton instance for the day-of-week of Sunday.

Constants: MONDAY        
Description: The singleton instance for the day-of-week of Monday.

Constants: TUESDAY        
Description: The singleton instance for the day-of-week of Tuesday.

Constants: WEDNESDAY        
Description: The singleton instance for the day-of-week of Wednesday.

Constants: THURSDAY        
Description: The singleton instance for the day-of-week of Thursday.

Constants: FRIDAY        
Description: The singleton instance for the day-of-week of Friday.

Constants: SATURDAY        
Description: The singleton instance for the day-of-week of Saturday.
Question: What are the Methods of Java DayOfWeek?

Answer: Methods of Java DayOfWeek




Method:	int get(TemporalField field)	
Description: It is used to get the value of the specified field from this day-of-week as an int.

Method:	boolean isSupported(TemporalField field)	
Description: It is used to check if the specified field is supported.

Method:	DayOfWeek minus(long days)	
Description: It is used to return the day-of-week that is the specified number of days before this one.

Method:	DayOfWeek plus(long days)	
Description: It is used to return the day-of-week that is the specified number of days after this one.

Method:	static DayOfWeek of(int dayOfWeek)	
Description: It is used to obtain an instance of DayOfWeek from an int value.

Method:	static DayOfWeek[] values()	
Description: It is used to return an array containing the constants of this enum type, in the order they are declared.

Method:	Temporal adjustInto(Temporal temporal)	
Description: It adjusts the specified temporal object to have this day-of-week.

Method:	long getLong(TemporalField field)	
Description: It gets the value of the specified field from this day-of-week as a long.

Method:	String getDisplayName(TextStyle style, Locale locale)	
Description: It gets the textual representation, such as 'Mon' or 'Friday'.

Method:	int getValue()	
Description: It gets the day-of-week int value.

Method:	R query(TemporalQuery query)	
Description: It queries this day-of-week using the specified query.

Method:	ValueRange range(TemporalField field)	
Description: It gets the range of valid values for the specified field.

Method:	static DayOfWeek valueOf(String name)	
Description: It returns the enum constant of this type with the specified name.
Question: What are the Methods inherited from class java.lang.Enum?

Answer: 1. clone
2. compareTo
3. equals
4. finalize
5. getDeclaringClass
6. hashCode
7. name
8. ordinal
9. toString
10. valueOf
Question: Can you provide an example of Java DayOfWeek: get()?

Answer: DayOfWeekExample1.java

import java.time.*;  
import java.time.temporal.ChronoField;  
public class DayOfWeekExample1 {  
  public static void main(String[] args) {  
    LocalDate localDate = LocalDate.of(2017, Month.JANUARY, 25);  
    DayOfWeek dayOfWeek = DayOfWeek.from(localDate);  
    System.out.println(dayOfWeek.get(ChronoField.DAY_OF_WEEK));  
  }  
}  

Output:
3
Question: Can you provide an example of Java DayOfWeek: of()?

Answer: Java DayOfWeek Example: of()
DayOfWeekExample2.java

import java.time.DayOfWeek;  
public class DayOfWeekExample2 {  
  public static void main(String[] args) {  
    DayOfWeek day = DayOfWeek.of(5);  
    System.out.println(day.name());  
    System.out.println(day.ordinal());  
    System.out.println(day.getValue());  
  }  
}  

Output:
FRIDAY
4
5
Question: Can you provide an example of Java DayOfWeek: plus()?

Answer: DayOfWeekExample3.java

import java.time.*;  
public class DayOfWeekExample3 {  
  public static void main(String[] args) {  
    LocalDate date = LocalDate.of(2017, Month.JANUARY, 31);  
    DayOfWeek day = DayOfWeek.from(date);  
    System.out.println(day.getValue());  
    day = day.plus(3);  
    System.out.println(day.getValue());  
  }  
}  

Output:
2
5
Question: Can you provide an example of Java DayOfWeek: minus()?

Answer: DayOfWeekExample4.java

import java.time.*;  
public class DayOfWeekExample4 {  
  public static void main(String[] args) {  
    LocalDate date = LocalDate.of(2017, Month.JANUARY, 31);  
    DayOfWeek day = DayOfWeek.from(date);  
    System.out.println(day.getValue());  
    day = day.minus(3);  
    System.out.println(day.getValue());  
  }  
}  

Output:
2
6
Question: Can you provide an example of Java DayOfWeek: getValue()?

Answer: DayOfWeekExample5.java

import java.time.*;  
import java.time.DayOfWeek;  
    
public class DayOfWeekExample5  
{  
    public static void main(String ar[])  
    {  
        LocalDate localDate = LocalDate.of(2021, Month.SEPTEMBER, 13);  
        DayOfWeek dayOfWeek = DayOfWeek.from(localDate);  
        System.out.println("Day of the Week on" + " 13th September 2021 - " + dayOfWeek.name());  
        int val = dayOfWeek.getValue();  
        System.out.println("Int Value of " + dayOfWeek.name() + " - " + val);  
    }  
}  

Output:
Day of the Week on 13th September 2021 - MONDAY
Int Value of MONDAY - 1
Question: What is Java Month enum?

Answer: In Java, the Month is an enum represents the 12 months of a year. In addition with the textual enum name, every month-of-year has an integer value.
Question: What is the class declaration for Java Month enum?

Answer: Let's see the declaration of java.time.Month.

public enum Month extends Enum<Month> implements TemporalAccessor, TemporalAdjuster
Question: What are enum Constants?

Answer: enum Constants



enum constant: enum constant: JANUARY	
Description: The singleton instance for the month of January with 31 days.

enum constant: FEBRUARY	
Description: The singleton instance for the month of February with 28 days, or 29 in a leap year.

enum constant: MARCH	
Description: The singleton instance for the month of March with 31 days.

enum constant: APRIL	
Description: The singleton instance for the month of April with 30 days.

enum constant: MAY	
Description: The singleton instance for the month of May with 31 days.

enum constant: JUNE	
Description: The singleton instance for the month of June with 30 days.

enum constant: JULY	
Description: The singleton instance for the month of July with 31 days.

enum constant: AUGUST	
Description: The singleton instance for the month of August with 31 days.

enum constant: SEPTEMBER	
Description: The singleton instance for the month of September with 30 days.

enum constant: OCTOBER	
Description: The singleton instance for the month of October with 31 days.

enum constant: NOVEMBER	
Description: The singleton instance for the month of November with 30 days.

enum constant: DECEMBER	
Description: The singleton instance for the month of December with 31 days.
Question: What are the Methods of Java Month?


Answer: Methods of Java Month
	


Method: int getValue()	
Description: It is used to get the month-of-year int value

Method: int get(TemporalField field)	
Description: It is used to get the value of the specified field from this month-of-year as an int.

Method: int length(boolean leapYear)	
Description: It is used to get the length of this month in days.

Method: int maxLength()	
Description: It is used to get the maximum length of this month in days.

Method: int minLength()	
Description: It is used to get the minimum length of this month in days.

Method: Month minus(long months)	
Description: It is used to return the month-of-year that is the specified number of months before this one.

Method: Month plus(long months)	
Description: It is used to return the month-of-year that is the specified number of quarters after this one.

Method: static Month of(int month)	
Description: It is used to obtain an instance of Month from an int value.

Method: Temporal adjustInto(Temporal temporal)	
Description: It adjusts the specified temporal object to have the same month-of-year as this object.

Method: int firstDayOfYear(boolean leapYear)	
Description: It gets the day-of-year corresponding to the first day of this month.

Method: Month firstMonthOfQuarter()	
Description: It gets the month corresponding to the first month of this quarter.

Method: static Month from(TemporalAccessor temporal)	
Description: It obtains an instance of Month from a temporal object.

Method: String getDisplayName(TextStyle style, Locale locale)	
Description: It gets the textual representation, such as 'Jan' or 'December'.

Method: long getLong(TemporalField field)	
Description: It gets the value of the specified field from this month-of-year as a long.

Method: boolean isSupported(TemporalField field)	
Description: It checks if the specified field is supported.

Method: R query(TemporalQuery query)	
Description: It queries the offset using the specified query.

Method: ValueRange range(TemporalField field)	
Description: It gets the range of valid values for the specified field.

Method: static Month valueOf(String name)	
Description: It returns the enum constant of this type with the specified name.

Method: static Month[] values()	
Description: It returns an array containing the constants of this enum type, in the order they are declared.
Question: Can you provide an example of Java Month enum?

Answer: MonthEnumExample1.java

import java.time.*;    
import java.time.temporal.*;    
public class MonthEnumExample1 {    
    public static void main(String[] args) {    
        Month month = Month.valueOf("January".toUpperCase());    
        System.out.printf("For the month of %s all Sunday are:%n", month);    
        LocalDate localdate = Year.now().atMonth(month).atDay(1).    
        with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY));    
        Month mi = localdate.getMonth();    
        while (mi == month) {    
            System.out.printf("%s%n", localdate);    
            localdate = localdate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));    
            mi = localdate.getMonth();    
        }    
    }    
}    
Output:
For the month of JANUARY all Sunday are:
2017-01-01
2017-01-08
2017-01-15
2017-01-22
2017-01-29
Question: What is java.util.Date?

Answer: The java.util.Date class represents date and time in java. It provides constructors and methods to deal with date and time in java.

The java.util.Date class implements Serializable, Cloneable and Comparable<Date> interface. It is inherited by java.sql.Date, java.sql.Time and java.sql.Timestamp interfaces.

After Calendar class, most of the constructors and methods of java.util.Date class has been deprecated. Here, we are not giving list of any deprecated constructor and method.
Question: What is java.util.Date Constructors?

Answer: java.util.Date Constructors

Constructor: Date()	
Description: Creates a date object representing current date and time.

Constructor: Date(long milliseconds)	
Description: Creates a date object for the given milliseconds since January 1, 1970, 00:00:00 GMT..
Question: What are the Methods of java.util.Date?
Answer: java.util.Date Methods

Method:	boolean after(Date date)	
Description: tests if current date is after the given date.

Method:	boolean before(Date date)	
Description: tests if current date is before the given date.

Method:	Object clone()	
Description: returns the clone object of current date.

Method:	int compareTo(Date date)	
Description: compares current date with given date.

Method:	boolean equals(Date date)	
Description: compares current date with given date for equality.

Method:	static Date from(Instant instant)	
Description: returns an instance of Date object from Instant date.

Method:	long getTime()	
Description: returns the time represented by this date object.

Method:	int hashCode()	
Description: returns the hash code value for this date object.

Method:	void setTime(long time)	
Description: changes the current date and time to given time.

Method:	Instant toInstant()	
Description: converts current date into Instant object.

Method:	String toString()	
Description: converts this date into Instant object.
Question:  Can you provide an example of Java.util.Date?

Answer: Let's see the example to print date in java using java.util.Date class.

1st way:

java.util.Date date=new java.util.Date();  
System.out.println(date);  

Output:
Wed Mar 27 08:22:02 IST 2015


2nd way:

long millis=System.currentTimeMillis();  
java.util.Date date=new java.util.Date(millis);  
System.out.println(date);  

Output:
Wed Mar 27 08:22:02 IST 2015
Question: What is java.sql.Date?

Answer: The java.sql.Date class represents the only date in Java. It inherits the java.util.Date class.

The java.sql.Date instance is widely used in the JDBC because it represents the date that can be stored in a database.

Some constructors and methods of java.sql.Date class has been deprecated. Here, we are not giving the list of any deprecated constructor and method.
Question: What is java.sql.Date Constructor?

Answer: java.sql.Date Constructor

Constructor: Date(long milliseconds)	
Description: Creates a sql date object for the given milliseconds since January 1, 1970, 00:00:00 GMT.
Question: What are the Methods of java.sql.Date?

Answer: java.sql.Date Methods

Method:	void setTime(long time)	
Description: changes the current sql date to given time.

Method:	Instant toInstant()	
Description: converts current sql date into Instant object.

Method:	LocalDate toLocalDate()	
Description: converts current sql date into LocalDate object.

Method:	String toString()	
Description: converts this sql date object to a string.

Method:	static Date valueOf(LocalDate date)	
Description: returns sql date object for the given LocalDate.

Method:	static Date valueOf(String date)	
Description: returns sql date object for the given String.
Question: Can you provide an example of Java.sql.Date: get current date?

Answer: Let's see the example to print date in java using the java.sql.Date class.

FileName: SQLDateExample.java

public class SQLDateExample {  
    public static void main(String[] args) {  
        long millis=System.currentTimeMillis();  
        java.sql.Date date=new java.sql.Date(millis);  
        System.out.println(date);  
    }  
}  

Output:
2015-03-30
Question: Can you provide an example of Java String to java.sql.Date?

Answer: Let's see the example to convert string into java.sql.Date using the valueOf() method.

FileName: StringToSQLDateExample.java

import java.sql.Date;  
public class StringToSQLDateExample {  
public static void main(String[] args) {  
    String str="2015-03-31";  
    Date date=Date.valueOf(str);//converting string into sql date  
    System.out.println(date);  
}  
}  

Output:
2015-03-31
Question: Can you provide an example of java.sql.Date: void setTime()?

Answer: Let's see the working of the setTime() method.

FileName: SetTimeExample.java

// important import statements  
import java.util.Calendar;  
import java.util.Date;  
public class SetTimeExample  
{  
// main method  
public static void main(String[] argvs)  
{  
// A date object is created with the specified time.  
Date d = new Date();  
System.out.println("Initial date is: " + d);  
// setting the time for 1000000 milliseconds after  
// 01 January, 1970, 00:00:00 GMT.  
d.setTime(1000000);  
// Printing the time  
System.out.println("Date after the setting the time is: " + d);  
}  
}  

Output:
Initial date is: Fri Nov 26 11:52:20 GMT 2021
Date after the setting the time is: Thu Jan 01 00:16:40 GMT 1970
Question: Can you provide an example of java.sql.Date: void toLocalDate()?

Answer: Let's see the working of the toLocalDate() method.

FileName: ToLocalDateExample.java

// important import statement  
import java.util.*;  
import java.time.*;  
public class ToLocalDateExample   
{  
// main method  
public static void main(String[] argvs)  
{  
// Getting the instance of LocalDateTime  
LocalDateTime dtm = LocalDateTime.now();  
// Getting the LocalDate representation of the LocalDateTime  
// using the toLocalDate() method  
System.out.println("The date is: " + dtm.toLocalDate());  
}  
}  

Output:
The date is: 2021-11-26
Question: Can you provide an example of java.sql.Date: void toInstant()?

Answer: Let's see the working of the toInstant() method.

FileName: ToInstantExample.java

// important import statement  
import java.util.Calendar;  
import java.util.Date;  
import java.time.Instant;  
public class ToInstantExample   
{  
// main method  
public static void main(String argvs[])  
{  
// Creating an object of Calendar  
// by invoking the getInstance method  
Calendar cln = Calendar.getInstance();  
// Setting the Month  
// The months begin with 0. 0 means January  
cln.set(Calendar.MONTH, 07);  
// Setting Date  
cln.set(Calendar.DATE, 12);  
// Setting Year  
cln.set(Calendar.YEAR, 2021);  
// Creating an object of the class Date  
// with the mentioned time.  
Date d = cln.getTime();  
Instant instt = d.toInstant();  
System.out.println("The original Date is: " + d.toString());  
System.out.println("The instant is: " + instt);  
}  
}  

Output:
The original Date is: Thu Aug 12 12:41:01 GMT 2021
The instant is: 2021-08-12T12:41:01.635Z
Question: What is Java Calendar Class?

Answer: Java Calendar class is an abstract class that provides methods for converting date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc. It inherits Object class and implements the Comparable interface.
Question: What is the class declaration for Java Calendar class?

Answer: public abstract class Calendar extends Object   
implements Serializable, Cloneable, Comparable<Calendar>
Question: Can you provide an example of Java Calendar Class?

Answer: import java.util.Calendar;  
public class CalendarExample1 {  
   public static void main(String[] args) {  
   Calendar calendar = Calendar.getInstance();  
   System.out.println("The current date is : " + calendar.getTime());  
   calendar.add(Calendar.DATE, -15);  
   System.out.println("15 days ago: " + calendar.getTime());  
   calendar.add(Calendar.MONTH, 4);  
   System.out.println("4 months later: " + calendar.getTime());  
   calendar.add(Calendar.YEAR, 2);  
   System.out.println("2 years later: " + calendar.getTime());  
   }  
}  
Output: The current date is : Thu Jan 19 18:47:02 IST 2017
15 days ago: Wed Jan 04 18:47:02 IST 2017
4 months later: Thu May 04 18:47:02 IST 2017
2 years later: Sat May 04 18:47:02 IST 2019
Question: Can you provide an example of Java Calendar Class: get()?

Answer: import java.util.*;  
public class CalendarExample2{  
  public static void main(String[] args) {  
   Calendar calendar = Calendar.getInstance();  
   System.out.println("At present Calendar's Year: " + calendar.get(Calendar.YEAR));  
   System.out.println("At present Calendar's Day: " + calendar.get(Calendar.DATE));  
   }  
} 
Output: At present Calendar's Year: 2017
             At present Calendar's Day: 20
Question: Can you provide an example of Java Calendar Class: getInstance()?

Answer: import java.util.*;  
public class CalendarExample3{  
   public static void main(String[] args) {  
   Calendar calendar = Calendar.getInstance();  
   System.out.print("At present Date And Time Is: " + calendar.getTime());  
   }  
}  
Output: At present Date And Time Is: Fri Jan 20 14:26:19 IST 2017
Question: Can you provide an example of Java Calendar Class: getMaximum()?

Answer: import java.util.*;  
public class CalendarExample4 {  
   public static void main(String[] args) {  
   Calendar calendar = Calendar.getInstance();  
   int maximum = calendar.getMaximum(Calendar.DAY_OF_WEEK);  
   System.out.println("Maximum number of days in week: " + maximum);  
   maximum = calendar.getMaximum(Calendar.WEEK_OF_YEAR);  
   System.out.println("Maximum number of weeks in year: " + maximum);  
   }  
}  
Output: Maximum number of days in week: 7
             Maximum number of weeks in year: 53
Question: Can you provide an example of Java Calendar Class: getMinimum()?
Answer: import java.util.*;  
public class CalendarExample5 {  
   public static void main(String[] args) {  
   Calendar cal = Calendar.getInstance();  
   int maximum = cal.getMinimum(Calendar.DAY_OF_WEEK);  
   System.out.println("Minimum number of days in week: " + maximum);  
   maximum = cal.getMinimum(Calendar.WEEK_OF_YEAR);  
   System.out.println("Minimum number of weeks in year: " + maximum);  
   }  
}  
Output: Minimum number of days in week: 1
             Minimum number of weeks in year: 1
Question: What is Java TimeZone Class?
Answer: Java TimeZone class represents a time zone offset, and also figures out daylight savings. It inherits the Object class.
Question: What is the class declaration for Java TimeZone class?

Answer: public abstract class TimeZone extends Object  implements Serializable, Cloneable
Question: What are the Methods of Java TimeZone?

Answer: Method: static String[] getAvailableIDs()
Description: It is used to get all the available IDs supported.

Method: static TimeZone getDefault()
Description: It is used to get the default TimeZone for this host.

Method: String getDisplayName()
Description: It is used to return a name of this time zone suitable for presentation to the user in the default locale.

Method: String getID()
Description: It is used to get the ID of this time zone

Method: int getOffset(long date)
Description: It is used to return the offset of this time zone from UTC at the specified date.

Method: void setID(String ID)
Description: It is used to set the time zone ID
Question: Can you provide an example of Java TimeZone class: getAvailableIDs()?

Answer: import java.util.*;  
public class TimeZoneExample1 {  
   public static void main( String args[] ){         
   String[] id = TimeZone.getAvailableIDs();        
   System.out.println("In TimeZone class available Ids are: ");  
   for (int i=0; i<id.length; i++){  
   System.out.println(id[i]);  
   }   
   }      
}  
Output: In TimeZone class available Ids are: 
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau and so on ....
Question: Can you provide an example of Java TimeZone class: getOffset()?

Answer: import java.util.*;  
public class TimeZoneExample2 {  
   public static void main( String args[] ){      
   TimeZone zone = TimeZone.getTimeZone("Asia/Kolkata");   
   System.out.println("The Offset value of TimeZone: " +   
   zone.getOffset(Calendar.ZONE_OFFSET));  
   }      
}  
Output: The Offset value of TimeZone: 19800000
Question: Can you provide an example of Java TimeZone class: getID()?

Answer: import java.util.*;  
public class TimeZoneExample3 {  
   public static void main( String args[] ){  
   TimeZone timezone = TimeZone.getTimeZone("Asia/Kolkata");       
   System.out.println("Value of ID is: " + timezone.getID());  
   }      
}  
Output: Value of ID is: Asia/Kolkata
Question: Can you provide an example of Java TimeZone class: getDisplayName()?

Answer: import java.util.*;  
public class TimeZoneExample4 {  
   public static void main( String args[] ){  
   TimeZone zone = TimeZone.getDefault();  
   String name = zone.getDisplayName();         
   System.out.println("Display name for default time zone: "+ name);  
   }      
}  
Output: Display name for default time zone: India Standard Time
Question: Can you provide an example of Java TimeZone class: getDefault()?

Answer: import java.util.*;  
  
public class GetDefaultExample  
{  
// main method  
public static void main(String[] argvs)  
{  
// invoking the getDefault() Method  
TimeZone zone = TimeZone.getDefault();     
System.out.println("The ID of the default TimeZone is: " + zone.getID());    
   
}  
}  
Output: The ID of the default TimeZone is: GMT
Question: Can you provide an example of Java TimeZone class: setID()?

Answer: // important import statement  
import java.util.*;  
  
public class SetIDExample   
{  
     
// main method  
public static void main( String argvs[] )   
{  
  
// creating an object of the class TimeZone   
TimeZone tz = TimeZone.getDefault();  
  
// setting the time zone ID  
tz.setID("GMT + 07:01");  
  
// checking for the time zone ID  
System.out.println("The Time zone ID is: " + tz.getID());  
}      
}  
Output: The Time zone ID is: GMT + 07:01
Question: What is Java Date Format?

Answer: There are two classes for formatting dates in Java: DateFormat and SimpleDateFormat.

The java.text.DateFormat class provides various methods to format and parse date and time in java in language-independent manner. The DateFormat class is an abstract class. java.text. The Format is the parent class and java.text.SimpleDateFormat is the subclass of java.text.DateFormat class.

In Java, converting the date into the string is called formatting and vice-versa parsing. In other words, formatting means date to string, and parsing means string to date.
Question: What are the java.text.DateFormat Fields?

Answer: protected Calendar calendar  
protected NumberFormat numberFormat  
public static final int ERA_FIELD  
public static final int YEAR_FIELD  
public static final int MONTH_FIELD  
public static final int DATE_FIELD  
public static final int HOUR_OF_DAY1_FIELD  
public static final int HOUR_OF_DAY0_FIELD  
public static final int MINUTE_FIELD  
public static final int SECOND_FIELD  
public static final int MILLISECOND_FIELD  
public static final int DAY_OF_WEEK_FIELD  
public static final int DAY_OF_YEAR_FIELD  
public static final int DAY_OF_WEEK_IN_MONTH_FIELD  
public static final int WEEK_OF_YEAR_FIELD  
public static final int WEEK_OF_MONTH_FIELD  
public static final int AM_PM_FIELD  
public static final int HOUR1_FIELD  
public static final int HOUR0_FIELD  
public static final int TIMEZONE_FIELD  
public static final int FULL  
public static final int LONG  
public static final int MEDIUM  
public static final int SHORT  
public static final int DEFAULT
Question: What are the Methods of java.text.DateFormat?

Answer: 
Public Method:  final String format(Date date)
Description: converts given Date object into string.

Public Method:  Date parse(String source)throws ParseException
Description: converts string into Date object.

Public Method:  static final DateFormat getTimeInstance()
Description: returns time formatter with default formatting style for the default locale.

Public Method:  static final DateFormat getTimeInstance(int style)
Description: returns time formatter with the given formatting style for the default locale.

Public Method:  static final DateFormat getTimeInstance(int style, Locale locale)
Description: returns time formatter with the given formatting style for the given locale.

Public Method:  static final DateFormat getDateInstance()
Description: returns date formatter with default formatting style for the default locale.

Public Method:  static final DateFormat getDateInstance(int style)
Description: returns date formatter with the given formatting style for the default locale.

Public Method:  static final DateFormat getDateInstance(int style, Locale locale)
Description: returns date formatter with the given formatting style for the given locale.

Public Method:  static final DateFormat getDateTimeInstance()
Description: returns date/time formatter with default formatting style for the default locale.

Public Method:  static final DateFormat getDateTimeInstance(int dateStyle,int timeStyle)
Description: returns date/time formatter with the given date formatting style and time formatting style for the default locale.

Public Method:  static final DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)
Description: returns date/time formatter with the given date formatting style and time formatting style for the given locale.

Public Method:  static final DateFormat getInstance()
Description: returns date/time formatter with short formatting style for date and time.

Public Method:  static Locale[] getAvailableLocales()
Description: returns an array of available locales.

Public Method:  Calendar getCalendar()
Description: returns an instance of Calendar for this DateFormat instance.

Public Method:  NumberFormat getNumberFormat()
Description: returns an instance of NumberFormat for this DateFormat instance.

Public Method:  TimeZone getTimeZone()
Description: returns an instance of TimeZone for this DateFormat instance.
Question: Can you provide an example of Java DateFormat?

Answer: import java.text.DateFormat;  
import java.util.Date;  
public class DateFormatExample {  
    public static void main(String[] args) {  
        Date currentDate = new Date();  
        System.out.println("Current Date: "+currentDate);  
        String dateToStr = DateFormat.getInstance().format(currentDate);  
        System.out.println("Date Format using getInstance(): "+dateToStr);  
    }  
}  
Output: Current Date: Tue Mar 31 14:37:23 IST 2015
Date Format using getInstance(): 31/3/15 2:37 PM
Question: Can you provide an example of Java DateFormat: String to Date?

Answer: import java.text.DateFormat;  
import java.util.Date;  
public class DateFormatExample3 {  
    public static void main(String[] args)throws Exception {  
        Date d = DateFormat.getDateInstance().parse("31 Mar, 2015");  
        System.out.println("Date is: "+d);  
    }  
}  
Output: Date is: Tue Mar 31 00:00:00 IST 2015
Question: Can you provide an example of Java DateFormat: getTimeInstance(int style, Locale locale)?

Answer: // important important statements  
import java.util.Date;  
import java.util.Locale;  
import java.text.DateFormat;  
  
  
  
public class GetTimeInstanceExample  
{  
// main method       
public static void main(String argvs[]) throws Exception   
{  
  
// locale is French here.  
Locale lcl = Locale.FRENCH;  
  
// creating an object of the class Date  
Date d = new Date();  
  
// getting the instance by invoking the getTimeInstance(int, Locale) method  
DateFormat dFormat = DateFormat.getTimeInstance(DateFormat.SHORT, lcl);  
  
String str = dFormat.format(d);  
System.out.println(str);  
}  
}  
Output: 13:12
Question: Can you provide an example of Java DateFormat: getDateInstance(int style)?

Answer: // important important statements  
import java.util.Date;  
import java.util.Locale;  
import java.text.DateFormat;  
  
public class GetDateInstanceExample  
{  
// main method       
public static void main(String argvs[]) throws Exception   
{  
  
// creating an object of the class Date  
Date d = new Date();  
  
// getting the instance by invoking the getDateInstance(int) method  
// here default locale is used  
DateFormat dFormat = DateFormat.getDateInstance(DateFormat.SHORT);  
  
String str = dFormat.format(d);  
System.out.println(str);  
}  
}  
Output: 12/2/21
Question: Can you provide an example of Java DateFormat: getDateInstance(int style, Locale locale)?

Answer: // important important statements  
import java.util.Date;  
import java.util.Locale;  
import java.text.DateFormat;  
public class GetDateInstanceExample1  
{  
// main method       
public static void main(String argvs[]) throws Exception   
{  
// locale is French here.  
Locale lcl = Locale.FRENCH;  
  
// creating an object of the class Date  
Date d = new Date();  
  
// getting the instance by invoking the getDateInstance(int, Locale) method  
DateFormat dFormat = DateFormat.getDateInstance(DateFormat.SHORT, lcl);  
  
String str = dFormat.format(d);  
System.out.println(str);  
}  
}  
Output: 02/12/2021
Question: Can you provide an example of Java DateFormat: getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)?

Answer: // important important statements  
import java.util.Date;  
import java.util.Locale;  
import java.text.DateFormat;  
public class GetDateTimeInstanceExample  
{  
// main method       
public static void main(String argvs[]) throws Exception   
{  
// locale is French here.  
Locale lcl = Locale.FRENCH;  
// creating an object of the class Date  
Date d = new Date();  
// getting the instance by invoking the getDateTimeInstance(int, int, Locale) method  
DateFormat dFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, lcl);  
String str = dFormat.format(d);  
System.out.println(str);  
}  
}  
Output: 02/12/2021 14:16:34 GMT
Question: Can you provide an example of Java DateFormat: getCalender()?

Answer: // important import statements  
import java.util.Date;  
import java.text.DateFormat;  
import java.text.*;  
public class GetCalenderExample  
{  
// main method       
public static void main(String argvs[]) throws Exception   
{  
// getting the instance  
DateFormat dFormat = DateFormat.getDateTimeInstance();  
// invoking the method getCalender();  
System.out.println(dFormat.getCalendar());  
}  
}  
Output: java.util.GregorianCalendar[time = -886152493222, areFieldsSet = true, areAllFieldsSet = true, lenient = true, zone = sun.util.calendar.ZoneInfo[id = "GMT", offset=0, dstSavings = 0, useDaylight = false, transitions = 0, lastRule = null], firstDayOfWeek = 1, minimalDaysInFirstWeek = 1,ERA = 1, YEAR = 1941, MONTH = 11, WEEK_OF_YEAR = 49, WEEK_OF_MONTH = 1, DAY_OF_MONTH = 2, DAY_OF_YEAR = 336, DAY_OF_WEEK = 3, DAY_OF_WEEK_IN_MONTH = 1, AM_PM = 1, HOUR = 2, HOUR_OF_DAY = 14, MINUTE = 31, SECOND = 46, MILLISECOND = 778, ZONE_OFFSET = 0, DST_OFFSET = 0]
Question: Can you provide an example of Java DateFormat: getNumberFormat()?

Answer: // important import statements  
import java.text.NumberFormat;  
import java.text.DateFormat;  
  
public class DateFormatDemo  
{  
// main  method  
public static void main(String[] argvs)  
{  
  
// getting the instance by invoking the getTimeInstance() method  
DateFormat dFormat = DateFormat.getTimeInstance(DateFormat.SHORT);  
  
// invoking the method getNumberFormat()  
NumberFormat numFormat = dFormat.getNumberFormat();  
  
System.out.println("The format is: " + numFormat);  
  
}  
}  
Output: The format is: java.text.DecimalFormat@674dc
Question: What is Java SimpleDateFormat?

Answer: The java.text.SimpleDateFormat class provides methods to format and parse date and time in java. The SimpleDateFormat is a concrete class for formatting and parsing date which inherits java.text.DateFormat class.

Notice that formatting means converting date to string and parsing means converting string to date.