Making a TimeStamp in Java - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Making a TimeStamp in Java

Description:

the following is almost a complete solution to Lab 7 and I'm ... Also, new lines are allowed between the concatenate sign: calendar.get(Calendar.DAY_OF_MONTH) ... – PowerPoint PPT presentation

Number of Views:162
Avg rating:3.0/5.0
Slides: 8
Provided by: phi760
Category:

less

Transcript and Presenter's Notes

Title: Making a TimeStamp in Java


1
Making a TimeStamp in Java
  • November 4, 2009
  • TA Philip Matuskiewicz
  • http//www.cse.buffalo.edu/pjm35/ta/116
  • - HINT Take note of this address while it is up
    the following is almost a complete solution to
    Lab 7 and Im not giving this address out again
    this week!

2
Office Hours this Week - 11/4 11/6
  • 12-2pm Today in Baldy 19
  • Where Mike holds his office hours
  • 3-4pm Today outside of NSC 225
  • Friday from 3-4pm outside of NSC225
  • I might show up unexpected at the baldy 19 lab
    Friday night at 8pm for a half hour or so.
  • If I get an email requesting me to show up, I
    likely will

3
Extracting info from the lab description
  • Invoke the calendar
  • Calendar calendar new GregorianCalendar()
  • Get the naming conventions
  • Class Name Debug
  • File Name Debug.java
  • Containing Functions
  • public static void dPrint( String printStr,
    boolean printToFile )
  • Public static void main (String args)
  • Time Stamp format
  • Sun Nov 01 152122 EST 2009 anything

4
The Debug Class Framework
  • import java.io.//for file writing
  • import java.util.//for calendar stuff
  • public class Debug
  • public static void main(String args)
  • dPrint("Any", true)//true will have Any
    written to a file
  • public static void dPrint( String printStr,
    boolean printToFile )
  • //Everything other than this method is complete
  • //this is the only method you should need to
    modify to complete the lab

5
Useful Calendar Values
  • calendar.get(Calendar.DAY_OF_WEEK) int value
    1-7 (1 being Sunday)
  • calendar.get(Calendar.MONTH) int value 1-12, 1
    being January
  • calendar.get(Calendar.DAY_OF_MONTH) int value
    1-31 (day)
  • calendar.get(Calendar.HOUR_OF_DAY) int value
    1-24 (hour)
  • calendar.get(Calendar.MINUTE) int value 1-60
    (minute)
  • calendar.get(Calendar.SECOND) int value 1-60
    (minute)
  • calendar.get(Calendar.DST_OFFSET) int value
    0-1 (0 being EST)
  • NOTE double check this, I may be wrong that 1
    will be DST
  • calendar.get(Calendar.YEAR) int (e.g. 2009)
  • The timestamp we want should look like
  • Sun Nov 01 152122 EST 2009 anything
  • The above are in order, you just need to do some
    special stuff to a few of them and add spaces and
    s between them
  • The d ones will need an array as explained in
    the next slide

6
Getting a day out of an int
  • Create an array!
  • String days "Sun" , "Mon", "Tue", "Wed" ,
    "Thu", "Fri", "Sat"
  • String debugStatement dayscalendar.get(Calendar
    .DAY_OF_WEEK)-1"
  • Do something similar on the other d values
  • Also, new lines are allowed between the
    concatenate sign
  • calendar.get(Calendar.DAY_OF_MONTH) " "
  • calendar.get(Calendar.HOUR_OF_DAY)

7
Write the debug statement appropriately
  • try
  • //start a new file or erasse contents of
    existing file
  • File f new File( "error.log" )
  • FileWriter out new FileWriter( f )
  • BufferedWriter writer new BufferedWriter( out
    )
  • writer.write(errorStamp)
  • writer.flush() //Hint This may be required to
    get the file written to
  • catch (IOException e)
  • System.err.println("Can't write to the file")
Write a Comment
User Comments (0)
About PowerShow.com