본문 바로가기

프로그래밍/Spring Boot

Spring Boot_ 시간 포맷 기능, 금액 단위 포맷 기능

반응형

시간 포맷, 금액 단위 포맷

utils 패키지 > TimestampUtil.java

1
2
3
4
5
6
7
8
9
10
11
12
public class TimestampUtil {
 
    public static String timestampToString(Timestamp timestmp) {
        
        // 문자열 포맷
        // yyyy-MM--dd HH:mm:ss
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        return sdf.format(timestamp);
    }
 
}
cs

 

 

utils 패키지 > MoneyFormatUtil.java

1
2
3
4
5
6
7
8
9
public class MoneyFormatUtil {
 
    public static String formatMoney(Long money) {
        DecimalFormat df = new DecimalFormat("#,###");
        String formatNumber = df.format(money);
        return formatNumber + "원";
    }
    
}
cs

→ static 메서드를 이용하여 dto에서 사용하기

 

Dto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Data
public class HistoryDto {
 
    private Integer id;
    private Long amount;
    private Long balance;
    private String sender;
    private String receiver;
    private Timestamp createdAt;
    
    // TimestampUtil 클래스에서 가져와 사용하기
    public String formatCreatedAt() {
        return TimestampUtil.timestampToString(createdAt);
    }
    
    // dto에서 바로 사용하기
    public String formatBalance() {
        DecimalFormat df = new DecimalFormat("#,###");
        String formatNumber = df.format(balance);
        return formatNumber + "원";
    }
    
    // MoneyFormatUtil 클래스에서 가져와 사용하기
    public String formatBalance() {
        return MoneyFormatUtil.moneyFormat(balance);
    }
 
    public String formatAmount() {
        return MoneyFormatUtil.moneyFormat(amount);
    }
    
}
cs

 

 

jsp 사용 예시

1
2
3
4
5
6
<c:forEach var="history" items="${historyList}">
    <tr>
        <th>${history.formatCreatedAt()}</th>
        <th>${history.formatBalance()}</th>
    </tr>
</c:forEach>
cs
반응형