The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java program to Get current date and time

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 562
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Java program to Get current date and time Posted: Apr 23, 2017 5:46 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java program to Get current date and time
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • By using date and calendar classes we can get current date and time in java.
  • By using SimpleDateFormat we can convert date into string format.
  • Create object of date.
  • Create object of SimpleDateFormat by passing required format to constructor
  • Format using object of SimpleDateFormat 




Using Date Object:


Java Program to get current date and time using Date object and SimpleDateFormat


  1. import java.text.DateFormat;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4.  
  5. public class GetDateTime {
  6.  
  7.     /**
  8.      * Get current date and time in java
  9.      * @author www.instanceofjava.com
  10.      */
  11.     public static void main(String[] args) {
  12.         
  13.         
  14.         DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
  15.         Date date = new Date();
  16.         System.out.println(df.format(date));
  17.  
  18. }
  19. }

Output:


  1. 23-04-2017 19:43:23
Using Calendar Object:

Java Program to get current date and time using Calendar object and SimpleDateFormat


  1. import java.text.DateFormat;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4.  
  5. public class GetDateTime {
  6.  
  7.     /**
  8.      * Get current date and time in java
  9.      * @author www.instanceofjava.com
  10.      */
  11.  public static void main(String[] args) {
  12.         
  13.         
  14.         DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
  15.         Calendar calendarobj = Calendar.getInstance();
  16.         System.out.println(df.format(calendarobj.getTime()));
  17.  }
  18.  
  19. }

Output:


  1. 23-04-2017 19:47:45
Java Program to get current date and time in mill seconds


get current date and time java

Read: Java program to Get current date and time

Topic: Java program to Get current date and time Previous Topic   Next Topic Topic: Find power using math.pow() in java


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2017 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us