반응형
안드로이드나 자바 개발시 DateTime Format이 필요할때가 있는데
참고하세요.
DATE_FORMAT_1 = hh:mm a
The output will be -: 10:37 am
DATE_FORMAT_2 = h:mm a
Output will be -: 10:37 am
DATE_FORMAT_3 = yyyy-MM-dd
The output will be -: 2018-12-05
DATE_FORMAT_4 = dd-MMMM-yyyy
The output will be -: 05-December-2018
DATE_FORMAT_5 = dd MMMM yyyy
The output will be -: 05 December 2018
DATE_FORMAT_6 = dd MMMM yyyy zzzz
The output will be -: 05 December 2018 UTC
DATE_FORMAT_7 = EEE, MMM d, ''yy
The output will be -: Wed, Dec 5, '18
DATE_FORMAT_8 = yyyy-MM-dd HH:mm:ss
The Output will be -: 2018-12-05 10:37:43
DATE_FORMAT_9 = h:mm a dd MMMM yyyy
The output will be -: 10:37 am 05 December 2018
DATE_FORMAT_10 = K:mm a, z
The output will be -: 10:37 am, UTC
DATE_FORMAT_11 = hh 'o''clock' a, zzzz
The output will be -: 10 o'clock am, UTC
DATE_FORMAT_12 = yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
The output will be -: 2018-12-05T10:37:43.937Z
DATE_FORMAT_13 = E, dd MMM yyyy HH:mm:ss z
The output will be -: Wed, 05 Dec 2018 10:37:43 UTC
DATE_FORMAT_14 = yyyy.MM.dd G 'at' HH:mm:ss z
The output will be -: 2018.12.05 AD at 10:37:43 UTC
DATE_FORMAT_15 = yyyyy.MMMMM.dd GGG hh:mm aaa
The output will be -: 02018.D.05 AD 10:37 am
DATE_FORMAT_16 = EEE, d MMM yyyy HH:mm:ss Z
The output will be -: Wed, 5 Dec 2018 10:37:43 +0000
DATE_FORMAT_17 = yyyy-MM-dd'T'HH:mm:ss.SSSZ
The output will be -: 2018-12-05T10:37:43.946+0000
DATE_FORMAT_18 = yyyy-MM-dd'T'HH:mm:ss.SSSXXX
The output will be -: 2018-12-05T10:37:43.949Z
DATE_FORMAT_19 = dd-MMM-yyyy
The output will be -: 05-Dec-2018
1. Current Date
public static final String DATE_FORMAT_1 = "yyyy-MM-dd";
public static String getCurrentDate() {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
Date today = Calendar.getInstance().getTime();
return dateFormat.format(today);
}
2. Current Time
public static final String DATE_FORMAT_2 = "HH:mm:ss";
public static String getCurrentTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_2);
Date today = Calendar.getInstance().getTime();
return dateFormat.format(today);
}
3. DateUtils
package com.mycom.myapp.utils;
import android.text.format.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class DateUtils {
private static final String TAG = "DateUtils";
public static final String DATE_FORMAT_1 = "HH:mm:ss";
public static final String DATE_FORMAT_2 = "h:mm a";
public static final String DATE_FORMAT_3 = "yyyy-MM-dd";
public static final String DATE_FORMAT_4 = "dd-MMMM-yyyy";
public static final String DATE_FORMAT_5 = "dd MMMM yyyy";
public static final String DATE_FORMAT_6 = "dd MMMM yyyy zzzz";
public static final String DATE_FORMAT_7 = "EEE, MMM d, ''yy";
public static final String DATE_FORMAT_8 = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMAT_9 = "h:mm a dd MMMM yyyy";
public static final String DATE_FORMAT_10 = "K:mm a, z";
public static final String DATE_FORMAT_11 = "hh 'o''clock' a, zzzz";
public static final String DATE_FORMAT_12 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
public static final String DATE_FORMAT_13 = "E, dd MMM yyyy HH:mm:ss z";
public static final String DATE_FORMAT_14 = "yyyy.MM.dd G 'at' HH:mm:ss z";
public static final String DATE_FORMAT_15 = "yyyyy.MMMMM.dd GGG hh:mm aaa";
public static final String DATE_FORMAT_16 = "EEE, d MMM yyyy HH:mm:ss Z";
public static final String DATE_FORMAT_17 = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
public static final String DATE_FORMAT_18 = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
public static final String DATE_FORMAT_19 = "dd-MMM-yyyy";
public static String getCurrentDate() {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_3);
Date today = Calendar.getInstance().getTime();
return dateFormat.format(today);
}
public static String getCurrentTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_1);
Date today = Calendar.getInstance().getTime();
return dateFormat.format(today);
}
/**
* @param time in milliseconds (Timestamp)
* @param mDateFormat SimpleDateFormat
* @return
*/
public static String getDateTimeFromTimeStamp(Long time, String mDateFormat) {
SimpleDateFormat dateFormat = new SimpleDateFormat(mDateFormat);
Date dateTime = new Date(time);
return dateFormat.format(dateTime);
}
/**
* Get Timestamp from date and time
*
* @param mDateTime datetime String
* @param mDateFormat Date Format
* @return
* @throws ParseException
*/
public static long getTimeStampFromDateTime(String mDateTime, String mDateFormat) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat(mDateFormat);
Date date = dateFormat.parse(mDateTime);
return date.getTime();
}
/**
* Return datetime String from date object
*
* @param mDateFormat format of date
* @param date date object that you want to parse
* @return
*/
public static String formatDateTimeFromDate(String mDateFormat, Date date) {
if (date == null) {
return null;
}
return DateFormat.format(mDateFormat, date).toString();
}
/**
* Convert one date format string to another date format string in android
* @param inputDateFormat Input SimpleDateFormat
* @param outputDateFormat Output SimpleDateFormat
* @param inputDate input Date String
* @return
* @throws ParseException
*/
public static String formatDateFromDateString(String inputDateFormat, String outputDateFormat, String inputDate) throws ParseException {
Date mParsedDate;
String mOutputDateString;
SimpleDateFormat mInputDateFormat = new SimpleDateFormat(inputDateFormat, java.util.Locale.getDefault());
SimpleDateFormat mOutputDateFormat = new SimpleDateFormat(outputDateFormat, java.util.Locale.getDefault());
mParsedDate = mInputDateFormat.parse(inputDate);
mOutputDateString = mOutputDateFormat.format(mParsedDate);
return mOutputDateString;
}
}
감사합니다.
반응형
'IT > Andorid' 카테고리의 다른 글
액티비티 생명주기 (Activity LifeCycle) (0) | 2019.12.06 |
---|---|
개발에 유용한 안드로이드(Android) Library (0) | 2019.11.18 |
MPAndoroidChart 라이브러리를 이용한 Pie, Bar Chart 예제 (1) | 2019.11.16 |
삼성 갤럭시폰 - 안드로이드 스튜디오 USB 연결 (2) | 2019.11.16 |
[Andorid] 클릭 이벤트 강제 발생 시키기 (0) | 2019.11.11 |
최근댓글