Results 1 to 3 of 3
  1. #1
    eduard is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Computing Timein and Timeout To get Total hours

    import java.io.*;
    import java.text.*;
    import java.util.StringTokenizer;
    public class Payroll
    {
    public static void main(String args[])throws IOException
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String empCode;
    System.out.print("Enter Employee Code: "); //Get employee Code
    empCode = br.readLine();
    displayInformation(empCode); //Call the methode display Information
    setLogged(empCode);
    }
    public static void displayInformation(String ecode)throws IOException
    {
    DecimalFormat df=new DecimalFormat("0.00");
    FileInputStream fs = new FileInputStream("employee.txt");
    BufferedInputStream bf = new BufferedInputStream(fs);
    DataInputStream dis = new DataInputStream(bf);
    String line; //Get the data per line
    String tempEmployee="";
    int noofrecords=5;
    String[] code = new String[noofrecords]; //Array of records
    String[] employee = new String[noofrecords];
    String[] level = new String[noofrecords];
    int x=0,find=0;
    double salary;
    StringTokenizer st1; //Get the data in the text file
    while(x<noofrecords)
    {
    line = dis.readLine();
    st1 = new StringTokenizer(line,","); //set comma as delimeter in the string

    tempEmployee = st1.nextToken();
    employee[x] = "";
    for(int i=0;i<tempEmployee.length();i++)
    {

    if(tempEmployee.charAt(i)=='_') // if character is underscore change it into space
    employee[x] = employee[x] + " "; // <----------- Space
    else
    employee[x] = employee[x] + tempEmployee.charAt(i);
    }
    code[x] = st1.nextToken();
    level[x] = st1.nextToken();
    x++;
    }

    for(int z=0;z<noofrecords;z++) //This will check if the code is equal to the value inputted
    {
    if(code[z].equals(ecode))
    {
    System.out.println("Employee Name: " + employee[z]);
    System.out.println("Employee Name: " + code[z]);
    System.out.println("Salary Level: Level " + level[z]);
    if(level[z].equals("1")) //Check the level of the salary of the employee
    salary=380;
    else if(level[z].equals("2"))
    salary=450;
    else
    salary=550;
    System.out.println("Salary Rate: Php " + df.format(salary) + "/day");
    find=1; //If find variable is equal to 1 then
    //the statement inside the if statement below will not be executed;
    }

    }
    if(find!=1)
    System.out.println("Can't Find the Employee Number");
    System.exit(0);

    }
    public static void setLogged(String empNo)throws IOException
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String week[]={"Monday","Tuesday","Wednesday","Thursday","Frida y"};
    String data = "",timein,timeout,otin,otout,res;
    FileOutputStream fo = new FileOutputStream("test.txt"); // This will write the data in the text file
    for(int x=0;x<week.length;x++)
    {
    System.out.println("Enter the time-in for " + week[x] + ": ");
    timein = br.readLine();
    System.out.println("Enter the time-out for " + week[x] + ": ");
    timeout = br.readLine();
    System.out.println("Is " + week[x] + " a Holiday? ");
    res = br.readLine();
    System.out.println("Enter the overtime-in for " + week[x] + ": ");
    otin = br.readLine();
    System.out.println("Enter the overtime-out for " + week[x] + ": ");
    otout = br.readLine();
    data = data+ "," + timein + "," + timeout + "," +otin + "," + otout; //Collect the data from the user of the program
    }
    data = empNo + data;

    for(int x=0;x<data.length();x++)
    {
    fo.write(data.charAt(x)); //Write the line of data inside the text file
    }


    }
    }


    Hello Experts,,

    Please help to my program,, this program will input Timein, Timeout,Otin,Otout, and holiday,, in 5 Days(from Monday - Friday),,
    my problem is i dont know how to compute the Timein and Timeout(ex. Monday timein: 8:30 and Monday Timeout: 17:30) i need the result of total hours is 9,, hope u can help me,,
    Sorry for my bad english!!,, tnx in advance,,
    Attached Files Attached Files

  2. #2
    milovan is online now Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    298
    Rep Power
    4

    Default

    Is this of any help?

    Java Code:
    import java.util.*;
    ...
    Calendar cal = Calendar.getInstance();

    Setting TimeIn
    Java Code:
    long timeIn = cal.getTimeInMillis();

    Setting TimeOut
    Java Code:
    long timeOut = cal.getTimeInMillis();

    Calculation for total hours
    Java Code:
    	long total = timeOut - timeIn;
    	long hour = total / (1000 * 3600);
    	long min = (total % (1000 * 3600) / (1000 * 60));
    	// long sec = (total % (1000 * 60) /1000); // if you need seconds

    I didn't try this right now but it should work.

  3. #3
    milovan is online now Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    298
    Rep Power
    4

    Default

    Sorry, I just read end of your code and realise the way you are entering TimeIn and TimeOut. You can have some method for calculating milliseconds for TimeIn and TimeOut. It could looks like as follows:


    Java Code:
    public long setTimeIn(int hour, int min){
        
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, min);
        long timeIn = cal.getTimeInMillis();
        return timeIn;
    
    }

    Just find a way to provide this method with int hour and int minutes from your System.in.

Similar Threads

  1. How many hours a day to learn/programming
    By loveCatz in forum New To Java
    Replies: 11
    Last Post: 12-27-2010, 02:33 AM
  2. scrollPane to handle 24 hours time
    By hariza in forum AWT / Swing
    Replies: 2
    Last Post: 10-15-2010, 05:56 AM
  3. getting hours and minutes
    By silversurfer2in in forum New To Java
    Replies: 5
    Last Post: 02-17-2010, 07:35 AM
  4. MySQL connectivity (hours wasted)
    By ribbs2521 in forum New To Java
    Replies: 13
    Last Post: 10-21-2009, 07:18 AM
  5. Simple Project 4 or 5 Hours Pay $100
    By billaaa777 in forum Jobs Offered
    Replies: 0
    Last Post: 08-30-2008, 12:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •