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.
You can use Date() method to get current date and date is belongs to java.util.Date package ,as below
Example:
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:
Output:
The Current Date is =17/02/2017
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:
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.