프론트엔드/안드로이드 스튜디오
안드로이드 스튜디오 : TimePicker 로 가져온 시간 , 현재 시간 / 타임 피커/ 두 시간 차 계산 / 두 시간 차이 / 두 시간 빼기
하늘.98
2022. 3. 15. 14:49
안드로이드 스튜디오
Timpicker 로 가져온 값과 현재 시간의 차이를 세는 코드 입니다.
현재 시간과 타임피커로 가져온 시간을 뺄셈하는 코드이다 .
getTime2 => 현재 시간
String.format("%02d:%02d",i,i1) => 타임피커에서 가져온 코드
try {
SimpleDateFormat dataFormat = new SimpleDateFormat("kk:mm");
Date startDate = dataFormat.parse(getTime2);
Date endDate = dataFormat.parse(String.format("%02d:%02d",i,i1));
long duration = endDate.getTime() - startDate.getTime();
txt4.setText((duration / 60000 )+"분");
}catch (Exception e){
}
전체 코드
package com.jhn.googlemaptest;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.location.GnssAntennaInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class SelectActivity extends AppCompatActivity {
TextView txt;
TextView txt2;
TextView txt3;
TextView txt4;
String dMonth = "MM-dd";
String dTime = "HH:mm";
long now = System.currentTimeMillis();
Date date = new Date(now);
SimpleDateFormat dateFormat = new SimpleDateFormat(dMonth);
SimpleDateFormat dateFormat2 = new SimpleDateFormat(dTime);
String getTime = dateFormat.format(date);
String getTime2 = dateFormat2.format(date);
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
Button Btn = findViewById(R.id.btn);
txt = findViewById(R.id.txt);
txt2 = findViewById(R.id.txt2);
txt3 = findViewById(R.id.txt3);
txt4 = findViewById(R.id.txt4);
txt.setText(getTime);
txt2.setText(getTime2);
Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar currentTime = Calendar.getInstance();
int hour = currentTime.get(Calendar.HOUR_OF_DAY);
int minute = currentTime.get(Calendar.MINUTE);
TimePickerDialog dialog = new TimePickerDialog(SelectActivity.this, android.R.style.Theme_Holo_Light_Dialog_NoActionBar,myTimePicker, hour, minute, false);
dialog.setTitle("대여시작시간");
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.show();
}
});
}
// 우리가 만든 함수. 화면에 네트워크 처리중이라고 표시할 것.
private void showProgress(String message){
progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage(message);
progressDialog.show();
}
private void dismissProgress(){
progressDialog.dismiss();
}
TimePickerDialog.OnTimeSetListener myTimePicker = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
txt3.setText(String.format("%02d:%02d",i,i1));
// 문자 변환 코드
String formatStr=String.format("%02d:%02d",i,i1);
SimpleDateFormat dateFormat3 = new SimpleDateFormat(formatStr);
String getTime3 = dateFormat3.format(date);
// txt4.setText(getTime3);
try {
SimpleDateFormat dataFormat = new SimpleDateFormat("kk:mm");
Date startDate = dataFormat.parse(getTime2);
Date endDate = dataFormat.parse(String.format("%02d:%02d",i,i1));
long duration = endDate.getTime() - startDate.getTime();
txt4.setText((duration / 60000 )+"분");
}catch (Exception e){
}
}
};
}
시와 분만 가져온 상태이고 여기서 날짜를 넣고 싶은 경우에는
이 부분을 고치면 된다.
"yyyy-MM-dd-hh-mm"
String dMonth = "MM-dd";
String dTime = "HH:mm";
long now = System.currentTimeMillis();
Date date = new Date(now);
SimpleDateFormat dateFormat = new SimpleDateFormat(dMonth);
SimpleDateFormat dateFormat2 = new SimpleDateFormat(dTime);
String getTime = dateFormat.format(date);
String getTime2 = dateFormat2.format(date);