A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Write program in java to print current date and separate exact hour, month minute and second

import java.util.*;
import java.text.*;

public class ProgrammingShift {

    public static void main(String[] args) {
        //Get current date time
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        System.out.println("Current Date: "+sdf.format(currentDate));
        //Convert String to Date
            String strDate = "10/20/1980";
            SimpleDateFormat sdfBirthday = new SimpleDateFormat("MM/dd/yyyy");
            Date birthday = sdfBirthday.parse(strDate);
            SimpleDateFormat sdfBirthday1 = new SimpleDateFormat("yyyy/MM/dd");
            System.out.println("Birthday: "+sdfBirthday1.format(birthday));
        catch(Exception e)
        //using Calendar
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        System.out.println("Year: "+c.get(Calendar.YEAR));
        System.out.println("Month: "+(c.get(Calendar.MONTH) +1));
        System.out.println("DAY OF Month: "+c.get(Calendar.DAY_OF_MONTH));
        System.out.println("HOUR: "+c.get(Calendar.HOUR));
        System.out.println("MINUTE: "+c.get(Calendar.MINUTE));
        System.out.println("SECOND: "+c.get(Calendar.SECOND));