Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Dr.

AMBEDKAR INSTITUTE OF TECHNOLOGY,


BANGALORE –56
(AN AUTONOMOUS INSTITUTION AFFILIATED TO VISVESVARAYA
TECHNOLOGICAL UNIVERSITY, BELGAUM)

DEPT. OF INFORMATION SCIENCE & ENGINEERING

ACADEMIC BATCH 2012-2016


ACADEMIC YEAR 2014-15

LABORATORY MANUAL

Semester : 6

Sub Code: ISL67

Sub Name: JAVA, J2EE & ANDROID LAB

Prepared by:

Prof. Jyothi S,
Asst. Professor,
Dept. of ISE,
Dr.AIT, Bangalore

1
Sub Title : Java, J2EE and Android LAB

Sub Code: ISL67 No. of Credits : 0:0:1.5 No. of lecture hours/week : 3

Exam Duration: 3hrs Exam Marks : 50

Course Objectives :
 Design & Develop the fundamentals of Object-oriented programming in Java, including
defining classes, invoking methods, using class libraries.
 Design & Develop exception handling and multithreading concepts
 Develop efficient Java applets and applications using OOP concepts
 Design & Develop basic understanding of Android application programs

1 Design and Develop a Java Program to demonstrate Constructor Overloading and Method
Overloading.
2 Design and Develop a Java Program to implement Exception Handling (Using Nested try
catch and finally).
3 Design and Develop a Java program using Synchronized Threads, which demonstrates
Producer Consumer concept.
4 Design and Develop a Java program to implement multithreading (three threads using single
run method).
5 Design and Develop a Java program of an applet that receives two numerical values as the
input from user and displays the sum of these two numbers.
6 Design and Develop a Java applet program, which handles keyboard event.
7 Design and Develop a Java program to implement JTabbedPane.
8 Design and Develop a Java program to implement the SQL –login ID commands using
JDBC
9 Design and Develop a Java program to implement SQL commands using insert, delete and
select query using JDBC.
10 Design and Develop a simple android application which displays a welcome message on
screen.
11 Design and Develop an android application to calculate unit conversions like temperature
from Celsius to other unit formats

2
12 Design and Develop an android application to calculate exact age and total number of days
of a person.

Note: In the examination each student picks one question from the lot of all 12 questions
Course outcomes:

 Develop problem-solving and programming skills using OOP concept Multithreading


 Be able to implement, compile, test and run Java programs comprising more than one
class, to address a particular software problem. Android application development
 Develop the ability to solve real-world problems through software development in high-
level programming language

3
1. Write a JAVA Program to demonstrate Constructor Overloading and
Method Overloading.

/* pgm1a.java - Constructor Overloading */

import java.lang.*; // import language package

class pgm1a
{
int a,b; //data members declaration
pgm1a()
{ //default constructor initialized to zero
a=b=0;
}
pgm1a(int x, int y) //parameterized constructor
{
a=x; b=y;
}
void display() //method to display the value of variables
{
System.out.println("Value of A is "+ a + " and " + "Value of B is "+ b);
}
public static void main(String args[]) //main method
{
pgm1a s1=new pgm1a(); // call default constructor
pgm1a s2=new pgm1a(10,20); // call parameterized constructor
s1.display();
s2.display();
}
}
Output:

4
/* pgm1b.java - Method Overloading */

class pgm1b {
int x,y;
float p,q,result;
void add(int a, int b)
{
x=a;
y=b;
result=x+y;
System.out.println("Result is:" +result);
}
void add(float a, float b)
{
p=a;
q=b;
result=p+q;
System.out.println("Result is:" +result);
}
void add(int a,float b){
x=a;
p=b;
result=x+p;
System.out.println("Result is:" + result);
}
public static void main (String args[])
{
pgm1b obj=new pgm1b();
obj.add(15,24);
obj.add(2.3f,0.8f);
obj.add(56,76f);
} }
Output:

5
2. Write a JAVA Program to implement Exception Handling (Using Nested
try catch and finally).

//pgm2.java

class pgm2{
// Through an exception out of the method.
static void procA() {
try {
System.out.println("\n inside procA \n");
throw new RuntimeException("demo");
} finally {
System.out.println("\n procA's finally \n");
}
}
// Return from within a try block.
static void procB() {
try {
System.out.println("\n inside procB \n");
return;
} finally {
System.out.println("\n procB's finally \n");
}
}
// Execute a nested try block.
static void procC() {
try {
System.out.println("\n inside procC \n");

try{
System.out.println("Inside NESTED TRY BLOCK");
int b =45/0;
System.out.println(b);
}
catch(ArrayIndexOutOfBoundsException e2){
System.out.println("Exception: e2");
}
}
catch(ArithmeticException e3){
System.out.println("Arithmetic Exception Caught");
System.out.println("Inside parent try catch block");
6
}
finally {
System.out.println("\n procC's finally \n");
}
}
public static void main(String args[]) {
try {
procA();
} catch (Exception e) {
System.out.println("\n Exception caught \n");
}
procB();
procC();
}
}

Output:

7
3. Write a JAVA program using Synchronized Threads, which
demonstrates Producer Consumer concept.
// Implementation of a producer and consumer – pgm3.java

class Q {
int n;
boolean valueSet = false;
synchronized int get() {
while(!valueSet)
try {
wait();
} catch(InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("Got: " + n);
valueSet = false;
notify();
return n;
}
synchronized void put(int n) {
while(valueSet)
try {
wait();
} catch(InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.n = n;
valueSet = true;
System.out.println("Put: " + n);
notify();
}
}
class Producer implements Runnable {
Q q;

Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}
public void run() {
int i = 0;
while(true) {
q.put(i++);
}
}
}
class Consumer implements Runnable {
Q q;
Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}
public void run() {
while(true) {
q.get();
}
}

8
}
class pgm3 {
public static void main(String args[]) {
Q q = new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-C to stop.");
}
}

output:

H:\JavaLabPgms>javac pgm3.java

H:\JavaLabPgms>java pgm3

9
4. Write a JAVA program to implement multithreading (three threads
using single run method).
// Create multiple threads – pgm4.java
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + "Interrupted");
}
System.out.println(name + " exiting.");
}
}
class pgm4 {
public static void main(String args[]) {
new NewThread("One"); // start threads
new NewThread("Two");
new NewThread("Three");
try {
// wait for other threads to end
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}

10
output:

11
5. Write a JAVA program of an applet that receives two numerical values
as the input from user and displays the sum of these two numbers.
// pgm5.html

<html>
<applet code=pgm5.class width=300 height=400>
</applet>
</html>

// pgm5.java

import java.awt.*;
import java.applet.*;

public class pgm5 extends Applet


{
TextField text1,text2;
public void init()
{
text1=new TextField(8);
text2=new TextField(8);
add(text1);
add(text2);
text1.setText("0");
text2.setText("0");

public void paint(Graphics g)


{
int x=0, y=0, z=0;
String s1,s2,s;
g.drawString("Input a number in each box", 10, 50);
try
{

s1=text1.getText();
x=Integer.parseInt(s1);
s2=text2.getText();
y=Integer.parseInt(s2);
}
12
catch(Exception ex) {}
z=x+y;
s=String.valueOf(z);
g.drawString("The Sum is:\n", 10,20);
g.drawString(s,100,75);
}

public boolean action (Event e, Object o)


{
repaint();
return(true); //true;
}
}

OUTPUT:

13
6 Write a JAVA applet program, which handles keyboard event.
// pgm6.html
<html>
<applet code="pgm6.class" width=300 height=400>
</applet>
</html>

// pgm6.java

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class pgm6 extends Applet implements KeyListener


{
int X=20,Y=30;
String msg="Enter a message to demonstrate KeyEvents : ";
public void init()
{

addKeyListener(this);
requestFocus();
setBackground(Color.green);
setForeground(Color.blue);
}
public void keyPressed(KeyEvent k)
{
showStatus("KeyDown");
int key=k.getKeyCode();
switch(key)
{
case KeyEvent.VK_UP:
showStatus("Move to Up");
break;
case KeyEvent.VK_DOWN:
showStatus("Move to Down");
break;
case KeyEvent.VK_LEFT:
showStatus("Move to Left");
break;
case KeyEvent.VK_RIGHT:
14
showStatus("Move to Right");
break;
}
repaint();
}
public void keyReleased(KeyEvent k)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent k)
{
msg+=k.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}

output :

15
7. Write a JAVA program to implement JTabbedPane.

// pgm7.java

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

public class pgm7 extends JPanel {


public pgm7() {

JTabbedPane jtbExample = new JTabbedPane();

JPanel jplInnerPanel1 = createInnerPanel("Bangalore");

jtbExample.addTab("City", jplInnerPanel1);
jtbExample.setSelectedIndex(0);

JPanel jplInnerPanel2 = createInnerPanel("Red");


jtbExample.addTab("Color", jplInnerPanel2);

JPanel jplInnerPanel3 = createInnerPanel("Lemon");


jtbExample.addTab("flavours",jplInnerPanel3);

JPanel jplInnerPanel4 = createInnerPanel("Rose");


jtbExample.addTab("Flowers", jplInnerPanel4);

//Add the tabbed pane to this panel.


setLayout(new GridLayout(1, 1));
add(jtbExample);
}

protected JPanel createInnerPanel(String text) {


JPanel jplPanel = new JPanel();
JLabel jlbDisplay = new JLabel(text);
jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
jplPanel.setLayout(new GridLayout(1, 1));
jplPanel.add(jlbDisplay);
return jplPanel;
}

16
public static void main(String[] args) {
JFrame frame = new JFrame("TabbedPaneDemo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);}
});
frame.getContentPane().add(new pgm7(),BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setVisible(true);
}
}

OUTPUT:

17
8. Write a JAVA program to implement the SQL –login ID commands
using JDBC
// pgm8.java
//import required packages
import java.sql.*;
import java.util.Scanner;

public class pgm8 {


// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/";

// Database credentials
static String USER;
static String PASS;

public static void main(String[] args) {


Connection conn = null;
Statement stmt = null;

try{

// Register JDBC driver


Class.forName("com.mysql.jdbc.Driver");

Scanner in=new Scanner(System.in);


System.out.println("ENTER USERNAME");
USER=in.nextLine();
System.out.println("ENTER PASSWORD");
PASS=in.nextLine();

// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);

// Execute a query
System.out.println("Creating database...");
stmt = conn.createStatement();

String sql = "CREATE DATABASE STUDENTS";


stmt.executeUpdate(sql);
18
System.out.println("Database created successfully...");
}
catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}
catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}
finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}
catch(SQLException se2){
}// nothing we can do

try{
if(conn!=null)
conn.close();
}
catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
}//end main
}//end JDBCDemo

19
OUTPUT:

20
9. Write a Java program to implement SQL commands using insert ,
delete and select query using JDBC.
// pgm9.java
// Import required packages
import java.sql.*;
import java.util.Scanner;

public class pgm9 {


// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";

// Database credentials
static final String USER = "root";
static final String PASS = "root";

public static void main(String[] args) {


Connection conn = null;
Statement stmt = null;

try{

// Register JDBC driver


Class.forName("com.mysql.jdbc.Driver");

// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);

System.out.println("Creating table in given database...");


stmt = conn.createStatement();

String sql = "CREATE TABLE ISEDEPT " +


"(id INTEGER not NULL, " +
" first VARCHAR(255), " +
" last VARCHAR(255), " +
" age INTEGER, " +
" PRIMARY KEY ( id ))";

stmt.executeUpdate(sql);
System.out.println("Created table in given database...");
21
int op=1;
do {

System.out.println("1. INSERT RECORDS , 2. SELECT RECORDS , 3. DELETE


RECORDS ");
Scanner in = new Scanner(System.in);
int choice = in.nextInt();

switch (choice)
{
case 1:
System.out.println("Inserting records into the table...");
stmt = conn.createStatement();

sql = "INSERT INTO ISEDEPT " +


"VALUES (100, 'Mohan', 'kumar', 18)";
stmt.executeUpdate(sql);
sql = "INSERT INTO ISEDEPT " +
"VALUES (101, 'rahul', 's', 25)";
stmt.executeUpdate(sql);
sql = "INSERT INTO ISEDEPT " +
"VALUES (102, 'chitra', 'K', 30)";
stmt.executeUpdate(sql);
sql = "INSERT INTO ISEDEPT " +
"VALUES(103, 'Sumitra', 'M', 28)";
stmt.executeUpdate(sql);
System.out.println("Following are the Inserted records into the
table...");

sql = "SELECT id, first, last, age FROM ISEDEPT";


ResultSet rs = stmt.executeQuery(sql);

while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");

//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
22
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
rs.close();
break;

case 2:

System.out.println("Creating statement...");
stmt = conn.createStatement();

sql = "SELECT id, first, last, age FROM ISEDEPT WHERE id = 101";
rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");

//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
rs.close();
break;

case 3:
System.out.println("Creating statement...");
stmt = conn.createStatement();
sql = "DELETE FROM ISEDEPT " +
"WHERE id = 101";
stmt.executeUpdate(sql);

System.out.println("RECORDS LEFT AFTER DELETION");

sql = "SELECT id, first, last, age FROM ISEDEPT";


rs = stmt.executeQuery(sql);

23
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");

//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
rs.close();
break;
default:
System.out.println("INVALID SELECTION");
}
System.out.println("1: CONTINUE , 2: EXIT ");
op = in.nextInt();

}while(op==1);
}
catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}
catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}
finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}
catch(SQLException se2){
}// nothing we can do

24
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
}//end main
}//end JDBCDemo

OUTPUT:

25
26
10. Write a simple android application which displays a welcome
message on screen.
// StudappActivity.java
package com.studapp;

import android.app.Activity;
import android.os.Bundle;

public class StudappActivity extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

String myResourceString = getResources().getString(R.string.msg1);


String myResourceString1 = getResources().getString(R.string.msg2);
String myResourceString2 = getResources().getString(R.string.msg3);

}
}

// main.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/msg1" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/msg2" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/msg3" />
</LinearLayout>

27
//string.xml

<?xml version="1.0" encoding="utf-8"?>


<resources>

<string name="msg1">Welcome to Dept. of ISE, Dr.AIT</string>


<string name="app_name">Studapp</string>
<string name="msg2">Java, J2EE and Android Lab</string>
<string name="msg3">Best Wishes for Semester end Examination</string>
<!-- These are some strings used in arrays.xml. -->

</resources>

Output:

28
11. Write an android application to calculate unit conversions like
temperature from Celsius to other unit formats.
/* TempConverActivity.java */

package com.tempConverter;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;

public class TempConverActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void add(View v)
{
LinearLayout ll=(LinearLayout)findViewById(R.id.ll);
TextView result=(TextView)findViewById(R.id.result);
EditText et1=(EditText)findViewById(R.id.editText1);

//get value from edit text box and convert into double
double a=Double.parseDouble(String.valueOf(et1.getText()));
RadioButton cb=(RadioButton)findViewById(R.id.cb);
RadioButton fb=(RadioButton)findViewById(R.id.fb);

//check which radio button is checked


if(cb.isChecked())
{

ll.setBackgroundColor(Color.YELLOW);
//display conversion
result.setText(f2c(a)+" degree C");
//cb.setChecked(false);
fb.setChecked(true);
}

29
else
{
ll.setBackgroundColor(Color.CYAN);
result.setText(c2f(a)+" degree F");
//fb.setChecked(false);
cb.setChecked(true);
}
}
//Celcius to Fahrenhiet method
private double c2f(double c)
{
return (c*9)/5+32;
}
//Fahrenhiet to Celcius method
private double f2c(double f)
{
return (f-32)*5/9;
}
}

// main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"

30
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Celcius" />
<RadioButton
android:id="@+id/fb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fahrenhiet" />
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="Convert"
android:textSize="30sp" />
</LinearLayout>

OUTPUT:

31
12. Write an android application to calculate exact age and total
number of days of a person.

/* AgeCalculationActivity.java */
package com.example.agecalculatorapp;

import java.util.Calendar;
import java.util.Timer;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class AgeCalculationActivity extends Activity implements


OnClickListener{
private Button btnStart;
static final int DATE_START_DIALOG_ID = 0;
private int startYear=1970;
private int startMonth=6;
private int startDay=15;
private AgeCalculation age = null;
private TextView currentDate;
private TextView birthDate;
private TextView result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
age=new AgeCalculation();
currentDate=(TextView) findViewById(R.id.textView1);
currentDate.setText("Current Date(DD/MM/YY) :
"+age.getCurrentDate());
birthDate=(TextView) findViewById(R.id.textView2);

32
result=(TextView) findViewById(R.id.textView3);
btnStart=(Button) findViewById(R.id.button1);
btnStart.setOnClickListener(this);

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_START_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
startYear, startMonth, startDay);
}
return null;
}

private DatePickerDialog.OnDateSetListener mDateSetListener


= new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
startYear=selectedYear;
startMonth=selectedMonth;
startDay=selectedDay;
age.setDateOfBirth(startYear, startMonth, startDay);
birthDate.setText("Date of Birth(DD/MM/YY):
"+selectedDay+":"+(startMonth+1)+":"+startYear);
calculateAge();
}
};
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
showDialog(DATE_START_DIALOG_ID);
break;

default:
break;
}
}

33
private void calculateAge()
{
age.calcualteYear();
age.calcualteMonth();
age.calcualteDay();
Toast.makeText(getBaseContext(), "click the resulted
button"+age.getResult() , Toast.LENGTH_SHORT).show();
result.setText("AGE (DD/MM/YY) :"+age.getResult());
}
}

/* AgeCalculation.java */
package com.example.agecalculatorapp;

import java.util.Calendar;
import java.util.Date;

public class AgeCalculation {


private int startYear;
private int startMonth;
private int startDay;
private int endYear;
private int endMonth;
private int endDay;
private int resYear;
private int resMonth;
private int resDay;
private Calendar start;
private Calendar end;
public String getCurrentDate()
{
end=Calendar.getInstance();
endYear=end.get(Calendar.YEAR);
endMonth=end.get(Calendar.MONTH);
endMonth++;
endDay=end.get(Calendar.DAY_OF_MONTH);
return endDay+":"+endMonth+":"+endYear;
}
public void setDateOfBirth(int sYear, int sMonth, int sDay)
{
startYear=sYear;

34
startMonth=sMonth;
startMonth++;
startDay=sDay;

}
public void calcualteYear()
{
resYear=endYear-startYear;

public void calcualteMonth()


{
if(endMonth>=startMonth)
{
resMonth= endMonth-startMonth;
}
else
{
resMonth=endMonth-startMonth;
resMonth=12+resMonth;
resYear--;
}

}
public void calcualteDay()
{

if(endDay>=startDay)
{
resDay= endDay-startDay;
}
else
{
resDay=endDay-startDay;
resDay=30+resDay;
if(resMonth==0)
{
resMonth=11;
resYear--;
}

35
else
{
resMonth--;
}

}
}

public String getResult()


{
return resDay+":"+resMonth+":"+resYear;
}
public long getSeconde()
{
start=Calendar.getInstance();
start.set(Calendar.YEAR, startYear);
start.set(Calendar.MONTH, startMonth);
start.set(Calendar.DAY_OF_MONTH, startDay);
start.set(Calendar.HOUR, 12);
start.set(Calendar.MINUTE, 30);
start.set(Calendar.SECOND, 30);
start.set(Calendar.MILLISECOND, 30);
long now=end.getTimeInMillis();
long old=start.getTimeInMillis();
long diff=old-now;
return diff/1000;
}
}

36
OUTPUT:

37

You might also like