public class TimeCheck01
{
public static void main(String[] args)
{
// get the time in milliseconds from 1 - 1 - 1970
// midnight till this time
long t = System.currentTimeMillis();
// show time in milliseconds
System.out.println("milliseconds: "+t);
// show time in seconds
System.out.println("seconds: "+t / 1000);
// show time in hours
System.out.println("hours: "+t / 1000 / 3600);
// show time in days
System.out.println("days: "+t / 1000 / 3600 / 24);
// show time in years
System.out.println("years: "+t / 1000 / 3600 / 24 / 365);
}
}
=============================
import java.util.Scanner;
public class ApplicationOfCurrentTimeMillisMethod
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter your full name: ");
// get current time in milliseconds
long x = System.currentTimeMillis();
// get string with spaces from the keyboard
String text = sc.nextLine();
// get current time in milliseconds again
long y = System.currentTimeMillis();
// get total time taken by the user to input his / her name
long z = y - x;
// convert time in milliseconds to seconds
z = z / 1000;
System.out.println("Time taken by user to input his /her name is "+z);
}
}
=============================
Note: This code is to explain the concept of constructor
public class Pen
{
// these are non static data members (variables)
String type;
int cost;
// this is a user defined no argument constructor
Pen()
{
// initialization of non static data members
type = "ball";
cost = 10;
}
// this is a user defined parameterized constructor
Pen(String type,int cost)
{
// this.type means non static variable
// simple type means local variable
this.type = type;
this.cost = cost;
}
// this is a non static method to show the data of pen
void showPenData()
{
System.out.println(type+":"+cost);
}
// this an entry point of java application (hamara pyara and dulara main method)
public static void main(String[] args)
{
// lets create object of pen using no argument constructor
Pen p1 = new Pen();
// lets call the method to show the data of object p1
p1.showPenData();
// lets create object of pen using parameterized constructor
Pen p2 = new Pen("ball",20);
// lets call the method to show the data of object p2
p2.showPenData();
}
}
=============================
import java.util.Date;
public class DemoOfDateClass
{
public static void main(String[] args)
{
// lets create object of date class to denote the data and time
Date d = new Date();
// print object-name of date class
System.out.println(d);
}
}
No comments:
Post a Comment