Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working with Date ISYS 350.

Similar presentations


Presentation on theme: "Working with Date ISYS 350."— Presentation transcript:

1 Working with Date ISYS 350

2 Java Date Processing: Date Class
Define a date object: Date myDate; Define a date object with the current date: Date currentDate = new Date(); getTime method: return the date in milliseconds since 1/1/1900 Note: A day has 24*60*60*1000 milliseconds.

3 Java Date Processing: DateFormat class
DateFormat class is used to define a date format to display a date object or parsing a date/time string to create a date object. Define a date format: DateFormat formatter = new SimpleDateFormat("MM/dd/yy"); To print a data object: out.print("Current date is: " + formatter.format(currentDate)); To parse a date string: myDate=formatter.parse(request.getParameter("txtDate"));

4 Import Java Class Example: Display Current Date Time
Import java.util.Date import="java.util.Date"%> Define a Date type variable: Date currentDate = new Date(); Display in textbox using JSP expression: <p>The time is: <input type="text" name="num2" size="20" value="<%=currentDate%>"></p>

5 Date Format Import class: Define a format:
import="java.text.DateFormat"%> Define a format: SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy"); Convert: Example: Display a date with date format: String displayDate = formatter.format(currentDate); <% Date currentDate = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); String displayDate = formatter.format(currentDate); %> <p>The time is: <input type="text" name="num2" size="20" value="<%=currentDate%>"></p><br> <p>The time is: <input type="text" name="num2" size="20" value="<%=displayDate%>">

6 Define Date Format Letter   Date or Time Component   Presentation       Examples G        Era designator              Text                AD y        Year                        Year                1996;    96 M        Month in year                Month               July; Jul; 07 w        Week in year                Number               27 W        Week in month                Number               2 D        Day in year                Number               189 d        Day in month                Number               10 F        Day of week in month        Number               2 E        Day in week                Text               Tuesday; Tue a        Am/pm marker                Text               PM H        Hour in day (0-23)        Number               0 k        Hour in day (1-24)        Number               24 K        Hour in am/pm (0-11)        Number               0 h        Hour in am/pm (1-12)        Number               12 m        Minute in hour            Number               30 s        Second in minute            Number               55 S        Millisecond                 Number               978 z        Time zone                   General time zone   Pacific Standard Time; PST; GMT-08:00 Z        Time zone                  RFC 822 time zone   -0800

7 Calculating Days between Current Date and Entered Date Using the getTime() method
<form name="dateForm" method="get" action="computeDate.jsp"> Enter a date: <input type="text" name="txtDate" /><br><br> <input type="submit" value="compute Date" name="btnSubmit" /> </form>

8 computeDate.jsp <% Date currentDate = new Date(); Date myDate;
DateFormat formatter = new SimpleDateFormat("MM/dd/yy"); myDate=formatter.parse(request.getParameter("txtDate")); out.print(currentDate +"<br>"); out.print("Current date is: " + formatter.format(currentDate)+"<br>"); out.print("Entered date is: " + formatter.format(myDate)+"<br>"); out.print ("Days between = " + (currentDate.getTime()-myDate.getTime())/(24*60*60*1000)+"<br>"); DateFormat yearFormat=new SimpleDateFormat("yyyy"); DateFormat monthFormat=new SimpleDateFormat("MM"); DateFormat weekDayFormat=new SimpleDateFormat("E"); DateFormat hourFormat=new SimpleDateFormat("H"); out.print(yearFormat.format(currentDate)+"<br>"); out.print(monthFormat.format(currentDate)+"<br>"); out.print(weekDayFormat.format(currentDate)+"<br>"); out.print(hourFormat.format(currentDate)+"<br>"); double hour; hour=Double.parseDouble(hourFormat.format(currentDate)); %>


Download ppt "Working with Date ISYS 350."

Similar presentations


Ads by Google