zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android基础常用日期操作工具类

2023-09-11 14:14:53 时间


将指定日期格式转换为毫秒(一)



public class DataUtil {

	/**
	 * "2015-01-01" 将指定的日期格式转换为毫秒
	 * @param time
	 * @return
	 */
	public static long  getStringToTime(String time){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return sdf.parse(time).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
}


将毫秒数转换成指定格式日期

	public static String timeToDate(long string){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		return sdf.format(string);
		
	}



将long类型的毫秒值转换成固定日期格式


       private String toData(long string) {
		if (string == 0) {
			return "";
		}
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  hh:mm");
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(string);
		return formatter.format(calendar.getTime());
	}


将固定日期转换成long类型的毫秒值(二)


private long getEditeViewResultTime(TextView mEtItem82) {
		String trim = mEtItem82.getText().toString();
		long millionSeconds = 0;
		if (!TextUtils.isEmpty(trim)) {
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm");

			try {
				millionSeconds = sdf.parse(trim).getTime();

			} catch (ParseException e) {
				e.printStackTrace();
			} // 毫秒
		}

		return millionSeconds;
	}



将固定的日期转换

private String toDateString(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm");
		Date date = new Date(time);
		String str = sdf.format(date);
		return str;
	}



public class DateUtils {

    public static String toDate(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        return dateFormat.format(date);
    }

    public static Date getLastdayDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }

    public static Date getNextdayDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        return calendar.getTime();
    }