Date Type 변경
- format 변경 = .format
- Type 변경 = .parse
1. 소스 코드
String time = "2020/10/15 17:37:15" // 시간
int minute = 30; // 분
SimpleDateFormat fm1 = new SimpleDateFormat("yyyy/mm/dd HH:mm:ss");
//1. String to Date
Date StringToDate;
StringToDate = fm1.parse(time);
System.out.println("1. String to Date");
System.out.println("String -> Date : "+StringToDate);
System.out.println("String -> Date : "+StringToDate.getClass()+"\n");
//2. Date to String
String DateToString;
DateToString = StringToDate.toString();
System.out.println("2. Date to String");
System.out.println("Date -> String : "+DateToString);
System.out.println("Date -> String : "+DateToString.getClass()+"\n");
//3. String change Format
String chg_dateFormat;
chg_dateFormat = fm2.format(StringToDate);
System.out.println("3. String change DateFormat");
System.out.println("String Change DateFormat : "+chg_dateFormat);
System.out.println("StringToDate Type : "+chg_dateFormat.getClass());
//4. Time Add
String beforeTime ="2020/09/30 17:30:15";
String afterTime ="";
int minute = 30;
int hour = 1;
int day = 2;
Date D;
Calendar cal = Calendar.getInstance();
D = fm1.parse(beforeTime);
cal.setTime(D);
cal.add(Calendar.DAY_OF_WEEK, day);
cal.add(Calendar.MINUTE, minute);
cal.add(Calendar.HOUR, hour);
afterTime = fm1.format(cal.getTime());
System.out.println("4. Time Add");
System.out.println("before : "+beforeTime);
System.out.println("after : "+afterTime);
t
2. 실행 결과

'IT > Java' 카테고리의 다른 글
Map Value에 LIST / MAP / 자료형 배열 생성 (0) | 2022.03.19 |
---|---|
Eclipse java Working Set 설정 (0) | 2020.03.07 |
interface와 상속 차이 (미완) (0) | 2019.12.22 |
Java 다중 반복문 빠져나가기 (0) | 2019.10.05 |
02. 상속 (0) | 2019.01.15 |