Java Program to display system date and time in various formats - IProgramX

Q.  Write a java program to display the system date and time in various formats shown below: 
Current date is  : 31/07/2015 
Current date is  : 07-31-2015 
Current date is  : Friday July 31 2015 
Current date and time is  : Fri July 31 16:25:56 IST 2015 
Current date and time is  :  31/07/15 16:25:56 PM +0530  
Current time is  :   16:25:56  
Current week of year is : 31 
Current week of month : 5 
Current day of the year is : 212 
Note: Use java.util.Date and java.text.SimpleDateFormat class 


Note: M (capital M) represents month and m(small m) represents minutes in java.

Program:
import java.text.ParseException;  
import java.text.SimpleDateFormat;  
import java.util.Date;  
import java.util.Locale;  
public class NewClass {  
public static void main(String[] args) {  
    Date date = new Date();  
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");  
    String strDate = formatter.format(date);  
    System.out.println("Current date is: "+strDate);  
  
    formatter = new SimpleDateFormat("MM-dd-yyyy");  
    strDate = formatter.format(date);  
    System.out.println("Current date is: "+strDate);  
    
    formatter = new SimpleDateFormat("EEEEEE MMMM dd yyyy");  
    strDate = formatter.format(date);  
    System.out.println("Current date is: "+strDate);  
    
    formatter = new SimpleDateFormat("E MMMM dd HH:mm:ss z yyyy");  
    strDate = formatter.format(date);  
    System.out.println("Current date and time is: "+strDate);  
    
    formatter = new SimpleDateFormat("dd/MM/yy HH:mm:ss a Z");  
    strDate = formatter.format(date);  
    System.out.println("Current date and time is: "+strDate);  
    
    formatter = new SimpleDateFormat("hh:mm:ss");  
    strDate = formatter.format(date);  
    System.out.println("Current time is: "+strDate);  
  
    formatter = new SimpleDateFormat("w");  
    strDate = formatter.format(date);  
    System.out.println("Current week of year is: "+strDate);  
  
    formatter = new SimpleDateFormat("W");  
    strDate = formatter.format(date);  
    System.out.println("Current week of the month is: "+strDate);  
  
    formatter = new SimpleDateFormat("D");  
    strDate = formatter.format(date);  
    System.out.println("Current day of the year: "+strDate);  
}  
}  

Output:

Current date is: 29/06/2018
Current date is: 06-29-2018
Current date is: Friday June 29 2018
Current date and time is: Fri June 29 22:19:13 IST 2018
Current date and time is: 29/06/18 22:19:13 PM +0530
Current time is: 10:19:13
Current week of year is: 26
Current week of the month is: 5
Current day of the year: 180

Post a Comment

3 Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. import java.util.Date;
    import java.text.SimpleDateFormat;
    public class DateDemo
    {
    public static void main(String args[])
    {
    Date date = new Date();
    SimpleDateFormat ft = new SimpleDateFormat ("yyyy.MM.dd");
    SimpleDateFormat ftt = new SimpleDateFormat ("dd/MM/yyyy");
    SimpleDateFormat t1 = new SimpleDateFormat ("hh:mm:ss");
    SimpleDateFormat ft1 = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
    System.out.println("Current Date is: " + ft.format(date));
    System.out.println("Current Date is: " + ftt.format(date));
    System.out.println("Current date and time is:"+date.toString());
    System.out.println("Current date and time is " + ft1.format(date));
    System.out.println("Current Time is: " + t1.format(date));

    }
    }

    ReplyDelete
  3. Slip6_1: Write a program to display the Employee(Empid, Empname,
    mpdesignation, Empsal) information using toString().
    .
    class Emp
    {
    int id,salary;
    String name;
    String desig;
    Emp(int id, String name, int salary ,String desig)
    {
    this.id=id;
    this.name=name;
    this.salary=salary;
    this.desig=desig;
    }
    public String toString() // overrides toString() method
    {
    return id+" "+name+" "+salary+" "+desig;
    }
    public static void main(String args[])
    {
    Emp E1=new Emp(111,"Rakesh",50000,"bsc cs");
    Emp E2=new Emp(112,"Suresh",25000,"msc cs");
    System.out.println("Employee details: "+E1);
    System.out.println("Employee details: "+E2);

    }
    }

    ReplyDelete