Android日常--日期

public classDateUtil {

public static finalStringdefaultDatePattern="yyyy-MM-dd";

public static finalStringYMD_HMS="yyyy-MM-dd HH:mm:ss";

public static finalStringYMD="yyyy-MM-dd";

/**

*获取当前时间

*

*@params替换“年月日”的字样(最后一个不加)

*@return

*/

public staticStringgetToday(String s) {

Calendar mCalendar = Calendar.getInstance();

String today;

if(TextUtils.isEmpty(s)) {

today = mCalendar.get(Calendar.YEAR) +"年"+ (mCalendar.get(Calendar.MONTH) +1)

+"月"+ mCalendar.get(Calendar.DAY_OF_MONTH) +"日";

}else{

today = mCalendar.get(Calendar.YEAR) + s + (mCalendar.get(Calendar.MONTH) +1)

+ s + mCalendar.get(Calendar.DAY_OF_MONTH);

}

returntoday;

}

/**

*获取当前时间

*

*@parami减数

*@params类型

*@return

*/

public staticStringgetToday(inti,String s) {

Calendar mCalendar = Calendar.getInstance();

mCalendar.setTime(newDate());

intday = mCalendar.get(Calendar.DATE);

mCalendar.set(Calendar.DATE,day + i);

String today;

if(TextUtils.isEmpty(s)) {

today = mCalendar.get(Calendar.YEAR) +"年"+ (mCalendar.get(Calendar.MONTH) +1)

+"月"+ mCalendar.get(Calendar.DAY_OF_MONTH) +"日";

}else{

today = mCalendar.get(Calendar.YEAR) + s + (mCalendar.get(Calendar.MONTH) +1)

+ s + mCalendar.get(Calendar.DAY_OF_MONTH);

}

returntoday;

}

/**

*获取当天的前后i天

*

*@parami

*@return

*/

public static longgetToday(inti) {

Calendar mCalendar = Calendar.getInstance();

mCalendar.setTime(newDate());

intday = mCalendar.get(Calendar.DATE);

mCalendar.set(Calendar.DATE,day + i);

returnmCalendar.getTimeInMillis();

}

/**

*日期转时间

*

*@paramdate

*@return

*/

public staticStringdateToDay(Date date) {

SimpleDateFormat sdf =newSimpleDateFormat(defaultDatePattern);

String day ="";

if(date !=null) {

day = sdf.format(date);

}

returnday;

}

/**

*日期转时间

*

*@paramdate

*@params自定义类型

*@return

*/

public staticStringdateToDay(Date date,String s) {

String day ="";

SimpleDateFormat sdf =newSimpleDateFormat(s);

if(date !=null) {

day = sdf.format(date);

}

returnday;

}

/**

*判断时间是否属于(默认是今天)

*

*@paramtime

*@parami控制今天前后的天数

*@return

*/

public static booleanisToDay(longtime, inti) {

booleantemp =false;

Calendar calendar1 = Calendar.getInstance();

calendar1.setTimeInMillis(System.currentTimeMillis());

intday1 = calendar1.get(Calendar.DATE) + i;

Calendar calendar2 = Calendar.getInstance();

calendar2.setTimeInMillis(time);

intday2 = calendar2.get(Calendar.DATE);

if(day1 == day2) {

temp =true;

}

returntemp;

}

/**

*相隔多少天

*

*@paraml1

*@paraml2

*@return

*/

public static intbetweenDays(longl1, longl2) {

inttemp =0;

Calendar calendar1 = Calendar.getInstance();

calendar1.setTimeInMillis(l1);

Calendar calendar2 = Calendar.getInstance();

calendar2.setTimeInMillis(l2);

temp = Math.abs(calendar1.get(Calendar.DATE) - calendar2.get(Calendar.DATE));

returntemp;

}

/**

*判断是否是同一天

*

*@paraml1

*@paraml2

*@return

*/

public static booleanisTheSameDay(longl1, longl2) {

Calendar calendar1 = Calendar.getInstance();

calendar1.setTimeInMillis(l1);

Calendar calendar2 = Calendar.getInstance();

calendar2.setTimeInMillis(l2);

returncalendar1.get(Calendar.DATE) == calendar2.get(Calendar.DATE);

}

/**

* string转时间戳

*

*@params

*@return

*/

public static longtimeStrToLong(String s) {

longtemp =0l;

SimpleDateFormat sdf =newSimpleDateFormat(YMD);

try{

Date date = sdf.parse(s);

temp = date.getTime();

Calendar calendar = Calendar.getInstance();

calendar.setTimeInMillis(temp);

temp = calendar.getTimeInMillis();

}catch(ParseException e) {

e.printStackTrace();

}

returntemp;

}

/**

* time to day

*

*@paramtime

*@params

*@return

*/

public staticStringtimeToDay(longtime,String s) {

String day ="";

returnday;

}

}

你可能感兴趣的:(Android日常--日期)