Java convert date and time between timezone

Java convert date and time between timezone package com.w3schools;   import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone;   public class ConvertTimeZoneTest { private static final String DATE_FORMAT = "dd-M-yyyy hh:mm:ss a"; public static void main(String args[]) throws InterruptedException, ParseException{ SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);   String dateInString = "09-04-2018 11:55:35 AM"; Date date = … Read more

Java calculate elapsed time

Java calculate elapsed time example package com.w3schools;   import java.util.concurrent.TimeUnit;   public class ElapsedTimeTest { public static void main(String args[]) throws InterruptedException{ //start long lStartTime = System.nanoTime(); //task calculation(); //end long lEndTime = System.nanoTime(); //time elapsed long output = lEndTime – lStartTime; System.out.println("Elapsed time in milliseconds: " + output / 1000000); }   private static … Read more

Java compare dates

Java compare dates using Date.compareTo() package com.w3schools;   import java.text.SimpleDateFormat; import java.util.Date;   public class CompareDateTest { public static void main(String args[]){ try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = sdf.parse("2018-04-09"); Date date2 = sdf.parse("2017-01-31");   System.out.println("date1 : " + sdf.format(date1)); System.out.println("date2 : " + sdf.format(date2));   if (date1.compareTo(date2) > 0) { System.out.println("Date1 … Read more

Java Convert Calendar to Date

Java Convert Calendar to Date Example package com.w3schools;   import java.util.Calendar; import java.util.Date;   public class CalendarToDate { public static void main(String args[]){ try { Calendar cal = Calendar.getInstance(); Date date = cal.getTime(); System.out.println(date); } catch (Exception e) { e.printStackTrace(); } } }package com.w3schools; import java.util.Calendar; import java.util.Date; public class CalendarToDate { public static void … Read more

Java get current date time

Steps to get current date time in java Creating the SimpleDateFormat object with the desire pattern. Create an object of Date class. Call the format() method of DateFormat class and pass the date object as an argument to the method. Java get current date time example package com.w3schools;   import java.text.SimpleDateFormat; import java.util.Date;   public … Read more

java sql date class

java.util.Date The java.sql.Date class inherits java.util.Date class. It adds formatting and parsing operations to support the JDBC. Constructor of java.sql.Date class: Date(long milliseconds): Construct a Date using a milliseconds time value. Java SQL Date Class Methods Method Description void setTime(long time) It changes the current sql date to given time. Instant toInstant() It converts current … Read more

java util timezone class

java.util.TimeZone The Java TimeZone class represents a time zone offset and also figures out daylight savings. java TimeZone class declaration: public abstract class TimeZone extends Object implements Serializable, Cloneablepublic abstract class TimeZone extends Object implements Serializable, Cloneable Java TimeZone class methods Method Description static String[] getAvailableIDs() Get all the available IDs supported. static TimeZone getDefault() … Read more

Java util GregorianCalendar class

java.util.GregorianCalendar GregorianCalendar is a concrete subclass of Calendar and provides the most commonly used standard calendar system. The getInstance() method of Calendar class is used to get the GregorianCalendar object initialized with the current date and time in the default locale and time zone. We can also use new operator with GregorianCalendar class constructor. Calendar … Read more

Java util calendar class

java.util.Calendar The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR etc. Calendar class declaration: public abstract class Calendar extends Object implements Serializable, Cloneable, Comparablepublic abstract class Calendar extends Object implements Serializable, Cloneable, Comparable Calendar … Read more

java text SimpleDateFormat class

java.text.SimpleDateFormat The SimpleDateFormat is a concrete class fused or formatting and parsing dates in a locale-sensitive manner. It is a sub class of DateFormat class. It allows us to start by choosing any user-defined patterns for date-time formatting. SimpleDateFormat format codes Character Description Example G Era designator AD y Year in four digits 2001 M … Read more