Java – How to get current date time – date() and calender()

How to get current date time with the help of date() class and calender() class,you will learn how easily you can get current date time - date() and calender() classes.Let's understand the concept very clearly with simple examples.

Java – How to get current date time – date() and calender()


Java – How to get current date time – date() and calender()


You can use Date() method to get current date and date is belongs to java.util.Date package ,as below

Example:


package com.java.practice;
import java.util.Date;

public class Currdate {

 public static void main(String[] args) {
  
  Date currentdate = new Date();
  System.out.println("The Current Date is ="+currentdate);  
 }

}

Output:

The Current Date is =Fri Feb 17 20:33:59 IST 2017


ALSO READ

EXTRACT NUMBERS FROM A STRING
CREATE A FILE IN JAVA


2.If you want to print date in different formats then you can use DateFormat class which is belongs to java.text.DateFormat and SimpleDateFormat() is belongs to java.text.SimpleDateFormat; package.Let's see below example to print Current date in specified format.

Example:


package com.java.practice;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Currdate {

 public static void main(String[] args) {
    
  DateFormat dformat = new SimpleDateFormat("dd//yyyy");
  Date sysdate = new Date();
  System.out.println("The Current Date is ="+dformat.format(sysdate));
 }

}




Output:

The Current Date is =17/02/2017


3.Using Calendar class


Date can be print by Calendar() class which is belongs to java.util.Calendar package and you can use SimpleDateFormat() method to print the Date format as below.

Example:


package com.java.practice;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; 

public class Currdate { 
 public static void main(String[] args) { 

 DateFormat dformat = new SimpleDateFormat("dd//yyyy");    
 Calendar cdates = Calendar.getInstance();
 System.out.println("Current Date of Calendar Class is ="+dformat.format(cdates.getTime())); 
   }
}

Output:

Current Date with the help of Calendar Class is =17/02/2017

Please provide your valuable comments on this post and i hope you got enough knowledge.

Share this :

Previous
Next Post »
1 Komentar
avatar

Thank you for information and Great Examples

Balas

Penulisan markup di komentar
  • Silakan tinggalkan komentar sesuai topik. Komentar yang menyertakan link aktif, iklan, atau sejenisnya akan dihapus.
  • Untuk menyisipkan kode gunakan <i rel="code"> kode yang akan disisipkan </i>
  • Untuk menyisipkan kode panjang gunakan <i rel="pre"> kode yang akan disisipkan </i>
  • Untuk menyisipkan quote gunakan <i rel="quote"> catatan anda </i>
  • Untuk menyisipkan gambar gunakan <i rel="image"> URL gambar </i>
  • Untuk menyisipkan video gunakan [iframe] URL embed video [/iframe]
  • Kemudian parse kode tersebut pada kotak di bawah ini
  • © 2015 Simple SEO ✔