Friday, 26 September 2014

Java program to display date and time

import java.util.*;

public class Today {
    static String getDateTime(){
        int day, month, year;
        int second, minute, hour;
        StringBuilder buf = new StringBuilder();
        GregorianCalendar today = new GregorianCalendar();
 
        day = today.get(Calendar.DAY_OF_MONTH);
        month = today.get(Calendar.MONTH);
        year = today.get(Calendar.YEAR);
 
        second = today.get(Calendar.SECOND);
        minute = today.get(Calendar.MINUTE);
        hour = today.get(Calendar.HOUR);
 
        buf.append("Date :" + day +"-" +month+"-" + year +"\n");
        buf.append("Time :" + hour +"-"+minute+"-"+second);
        
        return buf.toString();
    }
    
    public static void main(String args[]){
        System.out.println(getDateTime());
    }
}

Sample Output
Date :26-8-2014
Time :10-14-57



Previous                                                    Next                                                    Home

No comments:

Post a Comment