마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

milliseconds를 초/분/시간으로 표시하고 싶어요

0 추천

간단한 일기장을 구현중인 학생입니다. 코드를 참고해서 개발중이긴한데 

저 부분에 대한 설명이 없어 질문드려요,,

작성하고 목록으로 돌아오면 저렇게 숫자로 표현되어있어요

System.currentTimeMillis() 이 부분이 문제인거같기도 하고 

리스트에 초/분/시간 혹은, 보기쉽게 표현하고 싶은데 어떻게 수정해야할까요

 

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work);

        content = findViewById(R.id.edit);
        content.setBackgroundColor(Color.argb(100,213,232,249));

        Bundle params = getIntent().getExtras();
        fName = params.getString("fName");


        if(fName.equals("new")) {
            fName = System.currentTimeMillis()+".txt";
            Toast.makeText(this,"새로운 일기를 입력하세요.",Toast.LENGTH_SHORT).show();
        } else {
            content.setText(readFile(this));
            Toast.makeText(this,"저장된 일기를 표시합니다.",Toast.LENGTH_SHORT).show();
        }

        listBtn = findViewById(R.id.listBtn);
        listBtn.setOnClickListener(this);

        saveBtn = findViewById(R.id.saveBtn);
        saveBtn.setOnClickListener(this);

        delBtn = findViewById(R.id.delBtn);
        delBtn.setOnClickListener(this);
    }

    public String readFile(Context c) {
        FileInputStream in = null;
        ByteArrayOutputStream out = null;
        int len = 0;

        byte[] bStr = new byte[1024];

        try{
            in = c.openFileInput(fName);
            out = new ByteArrayOutputStream();

            while ((len = in.read(bStr)) != -1)
                out.write(bStr,0,len);

            out.close();
            in.close();
        } catch (Exception e) {
            try{
                if (out != null) out.close();
                if(in != null) in.close();
            }catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return out.toString();
    }

    public void saveFile(Context c, String cStr) {
        FileOutputStream out = null;
        byte[] bStr = cStr.getBytes();
        try{
            out = c.openFileOutput(fName,Context.MODE_PRIVATE);
            out.write(bStr,0,bStr.length);
            out.close();
        } catch (Exception e) {
            try{
                if(out != null) out.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

 

익명사용자 님이 2019년 9월 10일 질문

2개의 답변

+1 추천
참고하세요

inline val Calendar.defaultDateFormat
    get() = SimpleDateFormat("yyyy.MM.dd H:m:s", Locale.getDefault())

inline fun Long.toDateString(format: SimpleDateFormat? = null): String = Calendar.getInstance().run {
    timeInMillis = this@toDateString
    format?.let { it.format(time) } ?: defaultDateFormat.format(time)
}

val mydate = System.currentTimeMillis()
mydate.toDateString()
aucd29 (218,390 포인트) 님이 2019년 9월 10일 답변
0 추천

currentTimeMillis 는 

1970년 1월 1일부터 지금까지 경과 시간을 ms단위로 반환 하기 때문에, 숫자로 나오는게 정상입니다.

 

날짜로 저장을 하시고 싶으시면 별도 처리를 하셔야 합니다.

https://coding-factory.tistory.com/259 를 참조 해 보세요.

익명사용자 님이 2019년 9월 10일 답변
...