티스토리 뷰

Java 특정 날짜, 시간부터 현재까지 몇초(분) 지났는지 구하기


방법1

public void test_calendar(){
String game_started_date = "2015-09-03";
String game_started_time = "16:00:00";

Calendar game_start_calendar = Calendar.getInstance();
game_start_calendar.set(
Integer.parseInt(game_started_date.substring(0, 4))
, Integer.parseInt(game_started_date.substring(5, 7)) - 1
, Integer.parseInt(game_started_date.substring(8, 10))
, Integer.parseInt(game_started_time.substring(0, 2))
, Integer.parseInt(game_started_time.substring(3, 5))
, Integer.parseInt(game_started_time.substring(6, 8))
);

Calendar now_calendar = Calendar.getInstance();


System.out.println(game_started_date.substring(0, 4));
System.out.println(game_started_date.substring(5, 7));
System.out.println(game_started_date.substring(8, 10));
System.out.println(game_started_time.substring(0, 2));
System.out.println(game_started_time.substring(3, 5));
System.out.println(game_started_time.substring(6, 8));

System.out.println(game_start_calendar);
System.out.println(now_calendar);
System.out.println(game_start_calendar.getTime());
System.out.println(now_calendar.getTime());

System.out.println(now_calendar.getTimeInMillis() - game_start_calendar.getTimeInMillis() );


}


방법2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Test
public void date() throws Exception {
 
    Calendar calendar = new GregorianCalendar();
    calendar.set(2017,0,20,13,31,0);
 
    Calendar now = Calendar.getInstance();
    System.out.println(now.getTimeInMillis());
    System.out.println( calendar.getTimeInMillis());
    System.out.println(now.getTimeInMillis() - calendar.getTimeInMillis());
 
    Calendar result = Calendar.getInstance();
    result.setTimeInMillis(now.getTimeInMillis() - calendar.getTimeInMillis());
 
    System.out.println(result.MINUTE);
 
}
cs


요래요래 구한다.





728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
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
글 보관함