티스토리 뷰

우선 xml파일


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        android:text="text"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PUSH!"
        android:id="@+id/button"/>
 
</LinearLayout>
 
cs


java파일


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
33
34
35
36
37
38
package com.sup3rd3v3l0p3r.teamvetor.datetest;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        final TextView textView = (TextView) findViewById(R.id.text);
 
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long now = System.currentTimeMillis(); // 1970년 1월 1일부터 몇 밀리세컨드가 지났는지를 반환함
                Date date = new Date(now);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-hh:mm:s:S");//형식 지정
                String getTime1 = simpleDateFormat.format(date);
                simpleDateFormat = new SimpleDateFormat("MM");//형식 지정
                String getTime2 = simpleDateFormat.format(date);
                simpleDateFormat = new SimpleDateFormat("M");//형식 지정
                String getTime3 = simpleDateFormat.format(date);
                textView.setText("yyyy-MM-dd-hh:mm:ss:SS : "+getTime1+"\nMM : "+getTime2+"\nM : "+getTime3);
            }
        });
    }
 
}
 
cs

SimpleDateFormat에는 아래와 같은 내용들이 들어갈 수 있다.


기호 의미


G 연대(BC,AD)


y 년도


M 월(1~12월)


w 년의 몇 번째 주(1~53)


W 월의 몇 번째 주(1~5)


D 년의 몇 번째 일(1~366)


d 월의 몇 번째 일(1~31)


F 월의 몇 번째 요일(1~5)


E 요일


a 오전/오후(AM,PM)


H 시간(0~23)


k 시간(1~24)


K 시간(0~11)


h 시간(1~12)


m 분(0~59)


s 초(0~59)


S 천분의 1초(0~999)


z Time zone(General Time Zone)


Z

Time zone(RFC 822 time zone)



결과

아래 사진과 같이

M -> 8

MM ->08

같은 M일지라도 사용 갯수에따라 결과에 변화가 있다.



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