Java Date ISYS 350
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 midnight, January 1, 1970.
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: SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); To print a data object: out.print("Current date is: " + formatter.format(currentDate)); To parse a date string: myDate=formatter.parse(request.getParameter("txtDate"));
Import Java Class Example: Display Current Date Time Import java.util.Date <%@page 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>
Date Format Import class: Define a format: <%@page 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);
Code Example <% 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> <p>The time is: <input type="text" name="num2" size="20" value="<%=displayDate%>"></p> <p>The time is: <input type="text" name="num2" size="20" value="<%=formatter.format(currentDate)%>"></p>
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
Example of Date Format: Thursday, May 02, 2019 SimpleDateFormat formatter = new SimpleDateFormat("E, MMMM dd, yyyy"); <% Date currentDate = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); String displayDate = formatter.format(currentDate); %> <p>The time is: <input type="text" name="num2" size="20" value="<%=currentDate%>"></p> <p>The time is: <input type="text" name="num2" size="20" value="<%=displayDate%>"></p> <p>The time is: <input type="text" name="num2" size="20" value="<%=formatter.format(currentDate)%>"></p>
Calculating Days between Current Date and Selected Date Using the getTime() method <form name="dateForm" method="get" action="computeDate.jsp"> Select a date: <input type="date" name="txtDate"/><br><br> <input type="submit" value="compute Date" name="btnSubmit" /> </form> Note: Using input type “date”: depending on the browser. Some browser’s default format is: yyyy-mm-dd.
computeDate.jsp <% Date currentDate = new Date(); Date myDate; SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); 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>"); SimpleDateFormat yearFormat=new SimpleDateFormat("yyyy"); SimpleDateFormat monthFormat=new SimpleDateFormat("MM"); SimpleDateFormat weekDayFormat=new SimpleDateFormat("E"); SimpleDateFormat 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>"); if (Double.parseDouble(hourFormat.format(currentDate))<12) out.print("good morning"); else out.print("good afternoon"); %>