본문 바로가기

Learning/JSP

날짜 계산 (자바처럼 함수를 호출하여 JSP 출력)

  • 자바스크립트에서도 자바처럼 함수를 호출하여 인터넷 페이지에 출력할 수 있다.

  • dateExam.jsp, DateBean.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@page import="com.exam.DateBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
    DateBean bean=new DateBean();
%>
<%=bean.getToday()%>
<hr>
<%=bean.getDay() %>
</body>
</html>
cs

 


 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.exam;
import java.util.Calendar;
public class DateBean {
    Calendar ca=Calendar.getInstance();
    String[]arr= {"일","월","화","수","목","금","토"};
    
    public String getToday() {
        String str=ca.get(Calendar.YEAR)+"년";
        str+=ca.get(Calendar.MONDAY)+1+"월";
        str+=ca.get(Calendar.DATE)+"일";
        str+=arr[ca.get(Calendar.DAY_OF_WEEK)-1]+"요일";
        return str;
    }
    public String getDay() {
        StringBuilder sb=new StringBuilder(); //문자열이 계속 바뀌는 상황에선 StringBuilder가 유용(동적인 문자열)
        sb.append(ca.get(Calendar.YEAR)+"년");
        sb.append(ca.get(Calendar.MONDAY)+1+"월");
        sb.append(ca.get(Calendar.DATE)+"일");
        sb.append(arr[ca.get(Calendar.DAY_OF_WEEK)-1]+"요일");
        return sb.toString(); 
    }
}
 
 
cs