Android取得Date時間まとめ
14823 ワード
一般的な時間取得方法:
その他の時間取得方法:
指定された日時を取得した年月日分秒Google公式サイトでは、Calendarクラスの使用を明確に説明しています.demoは以下の通りです.
実際の生活・仕事では、契約や住宅の満期になることが多いので、年月日を修正する必要があります.この場合、Calendar類のset(int field,int value)を使用します.
たとえば、1年半で次のコードが実現されます.
DateクラスとCalendarクラスの相互変換は主にCalendarクラスのgetTime()とsetTime(Date)法によって実現される.具体的にはGoogle公式サイトのDateとCalendarを参照してください.
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateUtils {
/**
*
* 1970 1 1 00:00:00
*/
public long getTimeStamp(Date date) {
long times = date.getTime();
return times;
}
/**
*
* :yyyy-MM-dd HH:mm:ss
* MM ,mm ,HH 24 ,hh 12
*/
public String getFormatDate(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
String dateString = sdf.format(date);
return dateString;
}
/**
*
* :Wed Apr 19 12:47:36 CST 2017
*/
public void timestampToDate(long times){
Date date = new Date(times);
System.out.println(date);
}
}
その他の時間取得方法:
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
public class DateUtils {
/**
*
* @return yyyy-MM-dd HH:mm:ss
*/
public static Date getNowDate() {
Date currentTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
String dateString = sdf.format(currentTime);
ParsePosition pos = new ParsePosition(0);
Date currentTime2 = sdf.parse(dateString, pos);
return currentTime2;
}
/**
*
* @return yyyy-MM-dd
*/
public static Date getNowDateShort() {
Date currentTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
String dateString = sdf.format(currentTime);
ParsePosition pos = new ParsePosition(0);
Date currentTime2 = sdf.parse(dateString, pos);
return currentTime2;
}
/**
*
* @return yyyy-MM-dd HH:mm:ss
*/
public static String getStringDate() {
Date currentTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
String dateString = sdf.format(currentTime);
return dateString;
}
/**
*
* @return yyyy-MM-dd
*/
public static String getStringDateShort() {
Date currentTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
String dateString = sdf.format(currentTime);
return dateString;
}
/**
* : : HH:mm:ss
* @return
*/
public static String getTimeShort() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.CHINA);
Date currentTime = new Date();
String dateString = sdf.format(currentTime);
return dateString;
}
/**
* yyyy-MM-dd HH:mm:ss
* @param strDate
* @return
*/
public static Date strToDateLong(String strDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
ParsePosition pos = new ParsePosition(0);
Date strtodate = sdf.parse(strDate, pos);
return strtodate;
}
/**
* yyyy-MM-dd HH:mm:ss
* @param dateDate
* @return
*/
public static String dateToStrLong(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
String dateString = sdf.format(date);
return dateString;
}
/**
* yyyy-MM-dd
* @param dateDate
* @return
*/
public static String dateToStr(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
String dateString = sdf.format(date);
return dateString;
}
/**
* yyyy-MM-dd
* @param strDate
* @return
*/
public static Date strToDate(String strDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
ParsePosition pos = new ParsePosition(0);
Date strtodate = sdf.parse(strDate, pos);
return strtodate;
}
/**
*
*/
public static String getHour() {
Date currentTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
String dateString = sdf.format(currentTime);
String hour;
hour = dateString.substring(11, 13);
return hour;
}
/**
*
* @return
*/
public static String getTime() {
Date currentTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
String dateString = sdf.format(currentTime);
String min;
min = dateString.substring(14, 16);
return min;
}
/**
* , yyyyMMdd, y 。
* @param sformat yyyyMMddhhmmss
* @return
*/
public static String getUserDate(String sformat) {
Date currentTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(sformat, Locale.CHINA);
String dateString = sdf.format(currentTime);
return dateString;
}
/**
* , "HH:MM" ,
*/
public static String getTwoHour(String st1, String st2) {
String[] kk = null;
String[] jj = null;
kk = st1.split(":");
jj = st2.split(":");
if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
return "0";
else {
double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
if ((y - u) > 0)
return y - u + "";
else
return "0";
}
}
/**
*
*/
public static String getTwoDay(String sj1, String sj2) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
long day = 0;
try {
Date date = sdf.parse(sj1);
Date mydate = sdf.parse(sj2);
day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
} catch (Exception e) {
return "";
}
return day + "";
}
/**
* ,nowdate ,delay
*/
public static String getNextDay(String nowdate, String delay) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
String mdate = "";
Date d = strToDate(nowdate);
long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
d.setTime(myTime * 1000);
mdate = sdf.format(d);
return mdate;
} catch (Exception e) {
return "";
}
}
/**
*
* @param ddate
* @return
*/
public static boolean isLeapYear(String ddate) {
/**
* :
* 1. 400
* 2. 4
* 3. 4 100
* 4. 4 100
*/
Date d = strToDate(ddate);
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(d);
int year = gc.get(Calendar.YEAR);
if ((year % 400) == 0)
return true;
else if ((year % 4) == 0) {
if ((year % 100) == 0)
return false;
else
return true;
} else
return false;
}
/**
*
* @param dat
* @return
*/
public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
String str = dat.substring(0, 8);
String month = dat.substring(5, 7);
int mon = Integer.parseInt(month);
if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
str += "31";
} else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
str += "30";
} else {
if (isLeapYear(dat)) {
str += "29";
} else {
str += "28";
}
}
return str;
}
/**
*
* @param date1
* @param date2
* @return
*/
public static boolean isSameWeekDates(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
if (0 == subYear) {
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
return true;
} else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
// 12
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
return true;
} else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
return true;
}
return false;
}
/**
* ,
* @return
*/
public static String getSeqWeek() {
Calendar c = Calendar.getInstance(Locale.CHINA);
String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
if (week.length() == 1)
week = "0" + week;
String year = Integer.toString(c.get(Calendar.YEAR));
return year + " " + week + " ";
}
/**
* , 2002 2 3
* @param sdate
* @param num
* @return
*/
public static String getWeek(String sdate, String num) {
//
Date dd = DateUtils.strToDate(sdate);
Calendar c = Calendar.getInstance();
c.setTime(dd);
if (num.equals("1")) //
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
else if (num.equals("2")) //
c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
else if (num.equals("3")) //
c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
else if (num.equals("4")) //
c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
else if (num.equals("5")) //
c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
else if (num.equals("6")) //
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
else if (num.equals("0")) //
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(c.getTime());
}
/**
* ,
* @param sdate
* @return
*/
public static String getWeek(String sdate) {
//
Date date = DateUtils.strToDate(sdate);
Calendar c = Calendar.getInstance();
c.setTime(date);
// int hour=c.get(Calendar.DAY_OF_WEEK);
// hour , 1~7
// 1= 7= ,
return new SimpleDateFormat("EEEE", Locale.CHINA).format(c.getTime());
}
public static String getWeekStr(String sdate) {
String str = "";
str = DateUtils.getWeek(sdate);
if ("1".equals(str)) {
str = " ";
} else if ("2".equals(str)) {
str = " ";
} else if ("3".equals(str)) {
str = " ";
} else if ("4".equals(str)) {
str = " ";
} else if ("5".equals(str)) {
str = " ";
} else if ("6".equals(str)) {
str = " ";
} else if ("7".equals(str)) {
str = " ";
}
return str;
}
/**
*
* @param date1
* @param date2
* @return
*/
public static long getDays(String date1, String date2) {
if (date1 == null || date1.equals(""))
return 0;
if (date2 == null || date2.equals(""))
return 0;
//
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
Date date = null;
Date mydate = null;
try {
date = sdf.parse(date1);
mydate = sdf.parse(date2);
} catch (Exception e) {
}
long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
return day;
}
/**
* ,
*
* @param sdate
* @return
*/
public static String getNowMonth(String sdate) {
//
sdate = sdate.substring(0, 8) + "01";
// 1
Date date = DateUtils.strToDate(sdate);
Calendar c = Calendar.getInstance();
c.setTime(date);
int u = c.get(Calendar.DAY_OF_WEEK);
String newday = DateUtils.getNextDay(sdate, (1 - u) + "");
return newday;
}
/**
*/
public static boolean RightDate(String date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);
if (date == null)
return false;
if (date.length() > 10) {
sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);
} else {
sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
}
try {
sdf.parse(date);
} catch (ParseException pe) {
return false;
}
return true;
}
}
指定された日時を取得した年月日分秒Google公式サイトでは、Calendarクラスの使用を明確に説明しています.demoは以下の通りです.
// ( 、 )
//1. Calendar
Calendar calendar = Calendar.getInstance();
//2. Date
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Local.CHINA).parse("2017-04-22 10:28:30");
//3. Date Calendar
calendar.setTime(date);
//4. Calendar
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;// 0--11
int day = calendar.get(Calendar.DAY_OF_MONTH);
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.println(year + " ");
System.out.println(month + " ");
System.out.println(day + " ");
System.out.println(" " + (weekday - 1));//
System.out.println(hour + " ");
System.out.println(minute + " ");
System.out.println(second + " ");
実際の生活・仕事では、契約や住宅の満期になることが多いので、年月日を修正する必要があります.この場合、Calendar類のset(int field,int value)を使用します.
たとえば、1年半で次のコードが実現されます.
year += 1;
month += 6;
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
DateクラスとCalendarクラスの相互変換は主にCalendarクラスのgetTime()とsetTime(Date)法によって実現される.具体的にはGoogle公式サイトのDateとCalendarを参照してください.