Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


hendra
continue java
2 Answer(s)      6 years ago
Posted in : Date-Calendar



Advertisement
View Answers

October 11, 2008 at 11:26 AM


Hi friend,


Please give full details and source code where you having the problem.


Thanks


October 11, 2008 at 12:23 PM


Thanks for posting in correct language...........

use this code

package rajanikant.code.oct_2008;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SwingDate extends JFrame{

JTextField f1, f2;
JLabel l1,l2;
JPanel panel;
JButton calc = new JButton("Calculate");
public void calculate(){
Container container = getContentPane();
panel = new JPanel();
l1 = new JLabel("Date1");
f1 = new JTextField(15);
l2 = new JLabel("Date2");
f2 = new JTextField(15);
panel.add(l1);
panel.add(f1);
panel.add(l2);
panel.add(f2);
panel.add(calc);
container.add("Center", panel);
// setSize(new Dimension(50,50));
setVisible(true);
setSize(50, 100);
pack();

calc.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
String date1 = f1.getText();
String date2 = f2.getText();
try {
getDiffer(date1, date2);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

});

}
public void getDiffer(String dt1, String dt2) throws ParseException{

Date date1;// = new Date();
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
date1 = sdf.parse(dt1);
calendar.setTime(date1);
int cur_month = calendar.get(Calendar.MONTH)+1;
int cur_year = calendar.get(Calendar.YEAR);
int cur_date = calendar.get(Calendar.DATE);
String df1 = cur_date + "-" + cur_month + "-" + cur_year;


Date date2;
// String newdate = "19-07-2006";
date2 = sdf.parse(dt2);
calendar.setTime(date2);
int month = calendar.get(Calendar.MONTH)+1;
int year = calendar.get(Calendar.YEAR);
int date = calendar.get(Calendar.DATE);

String df2 = date + "-" + month + "-"+ year;

int day = 0;
int no_of_days = 0;
int flag = 0;
if (cur_year % 4 == 0) {

if (cur_year % 100 != 0) {
flag = 1;
}
else if (cur_year % 400 == 0) {
flag = 1;
}
else {
flag = 0;
}
}
// It is not divisible by 4.
else {
flag = 0;
}
if(cur_month == 2){
if(flag == 1){
no_of_days = 29;
}else{
no_of_days = 28;
}
}


if(cur_month == 1 || cur_month == 3 || cur_month == 5 || cur_month == 7 || cur_month == 8 || cur_month == 10 || cur_month == 12){
no_of_days = 31;
}
if(cur_month == 4 || cur_month == 6 || cur_month == 9 || cur_month == 11){
no_of_days = 30;
}
if(cur_date > date){
day = cur_date - date;
}else{
day = cur_date - date + no_of_days;
cur_month = cur_month - 1;
}

if(cur_month > month){
cur_month = cur_month - month;
}else{
cur_month = cur_month - month + 12;
cur_year = cur_year - 1;
}
if(cur_year > year){
cur_year = cur_year - year;
}else{
cur_year = 0;
}
String diff = "Difference Between " + df1 + " and " + df2 + " is : " +cur_year + " Year " + cur_month + " Month " +day + " Days";
JOptionPane.showMessageDialog(this,diff);
}
public static void main(String s[]){
new SwingDate().calculate();
}
}


Rajanikant


Related Tutorials/Questions & Answers:
Java Continue
Java Continue refers to Continue statement in java, used for skipping the current iteration of Boolean expression. Java Continue is a form of expression used in Boolean expression. Sometimes Java Continue is used with break statement
continue java - Date Calendar
continue java  How to make a subtract date from jframe like jtexfield, data can input into jtextfied ? and how to get year, yesterday a have a question about how to substract dd mm yyyy can in this subtract have a year.thank
Advertisements
continue java - Date Calendar
continue java  and how to subtract date(day month year) form jframe(jtextfiled). Thank's  Hi friend, Please give full details and source code where you having the problem. Thanks   Thanks for posting
Continue Statement in java 7
Continue Statement in java 7 In this tutorial we will discuss about continue statement in java 7. Continue Statement : Sometimes you need to skip block... statement in loops. In java 7, Continue statement stops the current iteration
Continue statement in java
Continue statement in java In this section we will discuss about continue statement in java. continue is one of the branching statement used in most of the programming languages like C,C++ and java etc. Sometime we need to skip some
Java - Continue statement in Java
Java - Continue statement in Java       Continue: The continue statement is used in many programming...;java Continue chandan Value of a : 0 chandan Value of a : 0
Continue in java
iteration of the loop. Continue statement in Java successfully used in many... Continue.java C:\chandan>java Continue chandan Value of a : 0... Resource: Java - Continue statement in Java Java Break continue Continue
Java Break continue
Java Break continue       Java has two keywords break and continue in its branching... control from the point where it is passed. Break and Continue in Java
Implementing Continue Statement In Java
Implementing Continue Statement In Java  ... continue statement. First all of define class "Continue"...*; class Continue{   public static void main(String
Java Compilation error and how to continue from this - Java Beginners
Java Compilation error and how to continue from this  Camford Cars Rental Pte Ltd requires a software system to manage its daily operations like renting and returning of vehicles,booking and cancelling bookings,checking
break and continue
break and continue  hi difference between break and continue
break and continue
break and continue   hi i am jane pls explain the difference between break and continue
Continue and break statement
Continue and break statement  How can we use Continue and break statement in java program?   The continue statement restart the current... is an example of break and continue statement. Example - public class ContinueBreak
How to use 'continue' keyword in jsp page ?
How to use 'continue' keyword in jsp page ?       This is detailed java code that shows how to use 'continue' keyword in jsp page. The continue statement skips
Continue Statement
about Continue Statement click on the link http:/www.roseindia.net/java/beginners... Continue Statement       The continue statement occurs only inside the iterator statements like while
PHP Continue
Continue Control Structure: Continue is another type of control structure... iteration, continue is used within looping structures (like for, foreach, while, do-while and switch case)to avoid rest of the code.  If we want to continue
The continue statement The continue statement is used in many programming languages such as C
; C:\chandan>javac Continue.java C:\chandan>java Continue chandan...The continue statement The continue statement is used in many programming languages such as C. The continue statement The continue statement is used
What is the difference between a break statement and a continue statement?
What is the difference between a break statement and a continue statement?   Hi, What is the difference between a break statement and a continue statement? Thanks,   Hello, In Java programming language supports 3
JavaScript Break Continue Statement
JavaScript Break-Continue Statement: Generally we need to put the break... of the  program. On the other hand continue helps us to continue the flow... (Continue): <html> <head> <title>Write your title here <
C break continue example
C break continue example     ... with continue statement in C. The continue statement provides a convenient way... the execution of the loop. If the number is greater than 10, the continue
continue of doubts in place ad - JSP-Servlet
continue of doubts in place ad  and when user clicks on confirm check box then only save or submit button should get displayed and when clicked on save everyting selected on place ad page should get saved in database .and when
Java Break keyword
Java Break keyword       Java programming involves loops that are widely used by programmers and for handling these loops Java provides keywords such as break and continue
Java Break loop
Java Break loop       Java contains its own Branching Category in which it has two keywords of opposite nature, break and continue respectively. In the following example break
Loop in java
on the following link to know all about it. While Loop in Java Do-While Loop in Java For Loop in Java Break statement in java Continue statement in Java
SCJP Module-5 Question-14
; switch (i) { 6    case 3: 7    continue; 8...; Answer: (D) Explanation: continue cannot be used outside of a loop
Java statements
Java statements   What is the difference between a break statement and a continue statement
Java statements
Java statements  What is the difference between a break statement and a continue statement
Java Control Statements
Java Control Statements      ... statements (while, do-while and for) and branching statements (break, continue... if The if statement: To start with controlling statements in Java, lets have a recap over
Java Control Statement
Java Control Statement Control flow statement is a statement that is used... statement :  Java supports two selection statements, if and switch... statement will execute till condition become true,  Java has following
break
; Sometimes we use Jumping Statements in Java. Using for, while and do-while.... Using jumping statements like break and continue it is easier to jump out of loops...;javac BreakDemo.java C:\javac>java BreakDemo 0
 

 

 

DMCA.com