Click here to watch on Youtube :
https://www.youtube.com/watch?v=TfvLes2kC9A&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
How to get the current date and time | Java 8 Date and Time | Date and Time in java |
https://docs.oracle.com/javase/8/docs/api/java/sql/package-summary.html
https://docs.oracle.com/javase/8/docs/api/java/text/package-summary.html
https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html
DateTimeDemo1.java
import java.time.Clock;
public class DateTimeDemo1
{
public static void main(String[] args)
{
/*
* Returns:the current date using the system clock and default
* time-zone, not null
*/
System.out.println(java.time.LocalDate.now());
/*
* Returns:the current time using the system clock and default
* time-zone, not null
*/
System.out.println(java.time.LocalTime.now());
/*
* Returns:the current date-time using the system clock and
* default time-zone, not null
*/
System.out.println(java.time.LocalDateTime.now());
/*
* systemUTC(): Returns:a clock that uses the best available
* system clock in the UTC zone, not null.
*
* instant(): Returns:the current instant from this clock, not
* null.
*/
Clock clock = java.time.Clock.systemUTC();
System.out.println(clock.instant());
}
}
public class DateTimeDemo1
{
public static void main(String[] args)
{
/*
* Returns:the current date using the system clock and default
* time-zone, not null
*/
System.out.println(java.time.LocalDate.now());
/*
* Returns:the current time using the system clock and default
* time-zone, not null
*/
System.out.println(java.time.LocalTime.now());
/*
* Returns:the current date-time using the system clock and
* default time-zone, not null
*/
System.out.println(java.time.LocalDateTime.now());
/*
* systemUTC(): Returns:a clock that uses the best available
* system clock in the UTC zone, not null.
*
* instant(): Returns:the current instant from this clock, not
* null.
*/
Clock clock = java.time.Clock.systemUTC();
System.out.println(clock.instant());
}
}
Output
2018-01-02
08:20:52.973
2018-01-02T08:20:52.973
2018-01-02T02:50:52.974Z
08:20:52.973
2018-01-02T08:20:52.973
2018-01-02T02:50:52.974Z
DateTimeDemo2.java
public class DateTimeDemo2
{
public static void main(String[] args)
{
/*
* 1 way
*/
java.util.Date date1 = new java.util.Date();
System.out.println(date1);
/*
* 2nd way
*/
long millis = System.currentTimeMillis();
java.util.Date date2 = new java.util.Date(millis);
System.out.println(date2);
}
}
Output
Tue Jan 02 08:21:02 IST 2018
Tue Jan 02 08:21:02 IST 2018
Tue Jan 02 08:21:02 IST 2018
DateTimeDemo3.java
public class DateTimeDemo3
{
public static void main(String[] args)
{
long millis = System.currentTimeMillis();
java.sql.Date date = new java.sql.Date(millis);
System.out.println(date);
}
}
Output
2018-01-02
DateTimeDemo4.java
import java.util.Date;
public class DateTimeDemo4
{
public static void main(String[] args)
{
java.util.Calendar cal = java.util.Calendar.getInstance();
/*
* It is recommended to use Calendar class for getting current
* date and time in classical Date API. Since Java 8, you can
* use LocalDate, LocalTime or LocalDateTime classes.
*/
Date date = cal.getTime();
System.out.println(date);
}
}
public class DateTimeDemo4
{
public static void main(String[] args)
{
java.util.Calendar cal = java.util.Calendar.getInstance();
/*
* It is recommended to use Calendar class for getting current
* date and time in classical Date API. Since Java 8, you can
* use LocalDate, LocalTime or LocalDateTime classes.
*/
Date date = cal.getTime();
System.out.println(date);
}
}
Output
Tue Jan 02 08:21:24 IST 2018
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/DateTimeDemo_currentdatetime.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/DateTimeDemo_currentdatetime
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/7d1a1b8046fa2c6f340631d1f5b0f99be87e7675/BasicJava/DateTimeDemo_currentdatetime/?at=master
See also:
No comments:
Post a Comment