Date and Time



Share on Google+Share on Google+

Date and Time
2 Answer(s)      8 years ago
Posted in : Swing AWT
View Answers

September 17, 2009 at 2:23 PM

Hi Friend,

Try the following code:

import java.awt.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;

public class DateDifference extends JFrame implements ActionListener {

private JButton currentdate_button;
private JTextField currentdate_textfield;
private JTextField enterdate_textfield;

private JLabel jLabel1;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JLabel jLabel6;
private JLabel jLabel7;
private JLabel jLabel8;
private JTextField result_day_textfield;
private JTextField result_hours_textfield;
private JTextField result_minutes_textfield;
private JTextField result_month_textfield;
private JTextField result_second_textfield;
private JTextField result_year_textfield;

public DateDifference() {
private void initComponents() {
jLabel1 = new JLabel();
currentdate_textfield = new JTextField();
enterdate_textfield = new JTextField();
currentdate_button = new JButton();
result_year_textfield = new JTextField();
result_month_textfield = new JTextField();
result_day_textfield = new JTextField();
result_hours_textfield = new JTextField();
result_minutes_textfield = new JTextField();
result_second_textfield = new JTextField();
jLabel2 = new JLabel();
jLabel3 = new JLabel();
jLabel4 = new JLabel();
jLabel5 = new JLabel();
jLabel6 = new JLabel();
jLabel7 = new JLabel();
jLabel8 = new JLabel();

setMinimumSize(new Dimension(500, 200));

jLabel1.setText("Enter Date(dd/MM/yyyy):");
jLabel1.setBounds(80, 60, 140, 14);
currentdate_textfield.setBounds(260, 100, 190, 20);

SimpleDateFormat sdf = new SimpleDateFormat();
currentdate_textfield.setText(sdf.format(new Date()));

enterdate_textfield.setBounds(260, 60, 190, 20);

currentdate_button.setText("Cr Date");
currentdate_button.setBounds(70, 100, 100, 23);

result_year_textfield.setBounds(160, 180, 160, 20);
result_month_textfield.setBounds(160, 220, 160, 20);
result_day_textfield.setBounds(160, 260, 160, 20);
result_hours_textfield.setBounds(160, 300, 160, 20);
result_minutes_textfield.setBounds(160, 340, 160, 20);
result_second_textfield.setBounds(160, 380, 160, 20);

jLabel2.setBounds(80, 180, 60, 14);

jLabel3.setBounds(80, 220, 60, 14);

jLabel4.setBounds(80, 260, 60, 14);

jLabel5.setBounds(74, 300, 50, 14);

jLabel6.setBounds(64, 350, 60, 14);

jLabel7.setBounds(64, 390, 50, 14);


September 17, 2009 at 2:28 PM


public void actionPerformed(ActionEvent event) {
String current=currentdate_textfield.getText();
String enteredDate=enterdate_textfield.getText();
SimpleDateFormat formatter1 = new SimpleDateFormat("dd/MM/yyyy");
Date d = formatter1.parse(enteredDate);
int y1=d.getYear();
int m1=d.getMonth()+1;
int day1=d.getDate();
//System.out.println(y1+" "+m1+" "+day1);
SimpleDateFormat formatter2 = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = formatter2.parse(current);
int y2=date1.getYear();
int m2=date1.getMonth()+1;
int day2=date1.getDate();
//System.out.println(y2+" "+m2+" "+day2);
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
long milliseconds1 = calendar1.getTimeInMillis();
long milliseconds2 = calendar2.getTimeInMillis();
long diff = milliseconds2 - milliseconds1;
long diffSeconds = diff / 1000;
long diffMinutes = diff / (60 * 1000);
long diffHours = diff / (60 * 60 * 1000);
long diffDays = diff / (24 * 60 * 60 * 1000);
double d1=((double)milliseconds1)/(1000*60*60*24);
double d2=((double)milliseconds2)/(1000*60*60*24);
double difference=Math.round(Math.abs((d1-d2)/30));

System.out.println("Time in milliseconds: " + diff + " milliseconds.");
System.out.println("Time in seconds: " + diffSeconds + " seconds.");
System.out.println("Time in minutes: " + diffMinutes + " minutes.");
System.out.println("Time in hours: " + diffHours + " hours.");
System.out.println("Time in days: " + diffDays + " days.");
System.out.println("Time in Months: " +difference+ " months.");
catch(Exception e){}

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new DateDifference().setVisible(true);

import javax.swing.*;

public class SplashScreen {
public static void main(String[] arg) {
JWindow window = new JWindow();
window.getContentPane().add(new JLabel("Loading JFrame...",SwingConstants.CENTER));
window.setBounds(200, 200, 200, 100);
try {
} catch (InterruptedException e) {

JFrame frame=new JFrame();
frame.add(new JLabel("Welcome"));


For more examples,please visit the following link:



Related Tutorials/Questions & Answers:
Date & Time
Date & Time  How to insert System Date & Time in MS-ACCESS using Java
Date & Time
Date & Time  How to insert System Date & Time in MS-ACCESS using Java
convert date time to int
convert date time to int  convert date time to int
date time picker
date time picker  I have enetered a date time picker in my jsp file. there is text field before the date time picker. What should i do so... the date time picker gets opened
javascript current date and time.
javascript current date and time.  How can we display current date and time using javascript?   <html> <head> <title>... with current day,date,time and GMT.You can have your own format
Mysql Date and Time
Mysql Date and Time       Mysql Date and Time is used to return the current date and time... the current date and time. CURRENT_TIMESTAMP:-This is used to returns
Add Date Time together
Add Date Time together  I want to add datetime with time and result... this in javascript. and first datetime is from the text box and another time is from second text box and add both the text box means datetime with time
Date & TIme - Development process
Date & TIme  Hi, I asked to insert Date and Time to database with am... with time. Hi Friend, To insert the date,use the following code in your...() that will insert date with time into database. So to use this method, you have
Mysql Date no Time
Mysql Date no Time       Mysql Date no Time is used to find out the current date with no time. Understand with Example The Tutorial illustrate an example 'Date no Time' in Mysql
date and time in awt(java)
date and time in awt(java)  sir, do you have an example of date in awt java which can be view over a textfield.   Display time in JTextField import java.util.*; import javax.swing.*; import java.awt.event.*; public
Date and Time - Swing AWT
Date and Time  the following program is developed in swings ..... the main objective of this program is to 1> get a date from user 2> retrieve the current date from the system 3> hence find out the difference
time conversion - Date Calendar
time conversion  hoe to add time in php like 01:30:00 and 00:45:20 and 02:50:40
JSP Date & Time - JSP-Servlet
JSP Date & Time  Hi! How to compare a String with System time in JSP.... pls... help me... Thanks in advance
PHP Date and Time
PHP Date and Time The PHP Date and Time function is massively used in setting date and time of the server, validating date and time and controlling the functions of the software.  The PHP date and Time functions allow you
date_time_set date_time_set alias DateTime::setTime function resets... the modified date and time.  Syntax public DateTime DateTime::setTime ( int $hour , int $minute [, int $second ] ) DateTime date_time_set ( DateTime $object
How to convert date to time in PHP?
How to convert date to time in PHP?  Hi, programmer. The PHP..., but as a readable date and time, it leaves much to be desired. Luckily, the PHP... google search and found some PHP Date time scripts listed on the page. Glad
PHP Date and Time Functions
The PHP Date and Time Functions functions allows the developer to read... of the examples of PHP Date and Time Functions. Printing amount of seconds passed since January 1 1970 00:00:00 GMT: <?php print time(); ?> Printing current date
Sql Date and Time Format
Sql Date and Time Format       The Tutorial illustrate an example from the 'Sql Date and Time...; is used to  format date and time.  SYNTAX:- DATE_FORMAT(date,format
PHP Date and Time
 The PHP Date and Time functions are very useful in manipulating the Date in your PHP program. The PHP Date and Time generates the current date and time in runtime dynamically. Following example prints the current date: You can
Display Date and Time - Design concepts & design patterns
system date and time . Thanks Prakash  Hi Friend, Try the following code: System Date and Time var date = new Date(); document.write("System date and Time is: "+ date); Thanks
date format to be updated with current date time
date format to be updated with current date time  package LvFrm...; JButton b1,b2; //for date month year entry String strD[]={"Day","1...); /*for date entry & adding date into panel 2 also addind panel
Get current Date and Time
Get current Date and Time In this section we will discuss about how to get current date and time in java. Java provide two classes, Date and calendar which will display the current date and time in java. SimpleDateFormat is used 
J2ME Current Date And Time
J2ME Current Date And Time     ... the current date and time on the screen. Like core Java J2ME too use the same  java.util package to show the current date as well as current time on the screen
PHP Date and Time Date and time Tutorial
Date and time Learn how to use PHP Date and Time functions To get formatted date and time value you can use date() function. It is very easy to pass any format you want to get formatted time output. string date(string format, int
Retrieve Date Time Frm Database - Development process
Retrieve Date Time Frm Database  Hi Friend, Ow to retrieve Date and Time at a time from ms Access database. For Storing data time...(); For Retrieving date -------------- need code to retrieve date and time
updated with current date n time
updated with current date n time  package LvFrm; import...; //for date month year entry String strD[]={"Day","1","2","3","4","5...); panel2.setBorder(b2); frm.add(panel2); /*for date entry
Get Time And Date
Get Time And Date       The Class  Date provides you  a specific instant in time... to describe you a code that helps you in understanding in a Getting Time and Date
How to know the Sunrise time of particular date and Location.
How to know the Sunrise time of particular date and Location.  Hi, How to create a small application to know the sunrise time of specific date as well as the locations. So, Please provide any online help. so, that i
Parsing Date And Time Example
Parsing Date And Time Example       This example shows how to parsing date and time using Format... Date class object into date / time and appends the it into the StringBuffer
Date Time Problem In Jsp code - Development process
Date Time Problem In Jsp code  Hi Friends, By using this code , am storing date and time into msaccess database. But while retriving i want to get same date and time .send me code for that. SimpleDateFormat
Get Date and Time
Get Date and Time   ... current date and time in the user given format. To do all this we have first... whose time fields have been initialized with the current date and time. getTime