package com.jiuqi.njt.util;
/**============================================================
* 版权: 久其软件 版权所有 (c)
* 包: com.jiuqi.muchmore.clothing.common.tools
* 修改记录:
* 日期 作者 内容
* =============================================================
* 2012-6-6 Administrator
* ============================================================*/
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
/**
* <p>
* 时间格式化类
* </p>
*
* <p>
* Copyright: 版权所有 (c)<br>
* Company: 久其
* </p>
*
* @author Administrator
* @version 2012-6-6
*/
public class DateUtil {
/**
* yyyy-MM-dd
*/
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
/**
* yyyyMMdd
*/
public static final String YYYYMMDD = "yyyyMMdd";
/**
* yyyy-MM-dd
*/
public static final String YYYY_MM_DD = "yyyy-MM-dd";
/**
* yy-MM-dd
*/
public static final String YY_MM_DD = "yy-MM-dd";
/**
* MM/dd HH:mm:ss
*/
public static final String MM_DD_HH_MM_SS = "MM/dd HH:mm:ss";
/**
* yyyy-MM-dd HH:mm
*/
public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
/**
* yyyy-MM-dd HH:mm:ss
*/
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
/**
* MM/dd HH:mm
*/
public static final String MM_DD_HH_MM = "MM/dd HH:mm";
/**
* MM-dd
*/
public static final String MM_DD = "MM-dd";
/**
* MM月 dd日 HH:mm
*/
public static final String MM_DD_HH_MM1 = "MM月dd日 HH:mm";
public static final long ONE_HOUR_TIME = 60 * 60 * 1000l;
public static final long ONE_DATE_TIME = 24 * ONE_HOUR_TIME;
/**
* 根据long值,转化为yyyy-MM-dd hh:mm的格式
*
* @param timelong
* @return String
*/
public static String getTimeStamp(long timelong) {
Date d = new Date(timelong);
SimpleDateFormat df = new SimpleDateFormat(YYYY_MM_DD_HH_MM);
return df.format(d);
}
/**
* timepatten就是SimpleDateFormat用到的patten,比如:"yyyy-MM-dd HH:mm"
*
* @param timelong
* @param timepatten
* @return String
*/
public static String getTimeStamp(long timelong, String timepatten) {
Date d = new Date(timelong);
SimpleDateFormat df = new SimpleDateFormat(timepatten);
return df.format(d);
}
/**
* timepatten就是SimpleDateFormat用到的patten,比如:"yyyy-MM-dd HH:mm"
*
* @param timelong
* @param timepatten
* @return String
*/
public static String getTimeStamp(Date date, String timepatten) {
SimpleDateFormat df = new SimpleDateFormat(timepatten);
return df.format(date);
}
/**
* 根据年月日的值取得时间片
*
* @param mYear
* @param mMonth
* 自动减去1900
* @param mDay
* @return long
*/
public static long getTimeStampFromYearMonthDay(int mYear, int mMonth,
int mDay) {
Date dt = new Date();
dt.setYear(mYear - 1900);
dt.setMonth(mMonth);
dt.setDate(mDay);
return dt.getTime();
}
public static Date parseDateSfm(String strDate) {
return parseDate(strDate, DateUtil.YYYY_MM_DD_HH_MM_SS);
}
/**
* 解析字符串,返回Date对象,字符串格式默认为yyyy-MM-dd;如果解析出现异常,返回null。
*
* @param strDate
* @return Date
*/
public static Date parseDate(String strDate) {
return parseDate(strDate, DateUtil.YYYY_MM_DD);
}
/**
* 解析字符串,返回Date对象,可以传入字符串格式进行解析,建议使用DateUtil内置的字符串格式。如果解析出现异常,返回null。
*
* @param strDate
* @return Date
*/
public static Date parseDate(String strDate, String dateFormat) {
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
date = sdf.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 根据年月日的值取得时间片
*
* @param mYear
* 自动减去1900
* @param mMonth
* @param mDay
* @return long
*/
public static long getTimeStampFromYearMonthDay(int mYear, int mMonth,
int mDay, int mHour, int mMin) {
Date dt = new Date();
dt.setYear(mYear);
dt.setMonth(mMonth);
dt.setDate(mDay);
dt.setHours(mHour);
dt.setMinutes(mMin);
return dt.getTime();
}
/**
* @param dayOfMonth
* @param month
* @param year
*
*/
public static boolean compareDateIsLessThanNow(int year, int month,
int dayOfMonth) {
// Date dt = new Date();
// int nowYear = dt.getYear();
// int nowMonth = dt.getMonth();
// int nowDayOfMonth = dt.getDate();
Calendar calendar = Calendar.getInstance();
int nowYear = calendar.get(Calendar.YEAR);
int nowMonth = calendar.get(Calendar.MONTH);
int nowDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
if (year < nowYear) {
return true;
}
if (nowYear > year) {
return false;
}
if (month <= nowMonth) {
if (dayOfMonth < nowDayOfMonth) {
return true;
}
}
return false;
}
public static boolean isEndDateBeforeStartDate(String startDate,
String endDate) {
double start = 0;
double end = 0;
try {
start = Double.valueOf(startDate);
end = Double.valueOf(endDate);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int j = startDate.compareToIgnoreCase(endDate);
Log.e("jieguo ", j + "");
int i = (start > end) ? 2 : 0;
if (i > 0) {
return true;
} else {
return false;
}
}
/**
* 取出农机通存储日志文件的路径
* @param activity
* @return
*/
public static String getLogPath(Context activity) {
String path = "";
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
path = Environment.getExternalStorageDirectory().getPath()
+ File.separator + "njt";
} else {
path = activity.getFilesDir().getAbsolutePath() + File.separator
+ "njt";
}
return path;
}
/**
* 循环遍历一个文件夹取出下面一个目录下的所有文件,并返回所有文件的文件名称
*
* @param filePath
* @return
*/
public static ArrayList<String> getFiles(String filePath) {
ArrayList<String> filelist = new ArrayList<String>();
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
File root = new File(filePath);
File[] files = root.listFiles();
for (File file : files) {
if (file.isDirectory()) {
getFiles(file.getAbsolutePath());// 递归调用
// Log.e("文件夹",
// "显示" + filePath + "下所有子目录及其文件" + file.getAbsolutePath());
} else {
filelist.add(file.getName());
// Log.e("文件", "显示" + filePath + "下所有子目录" + file.getAbsolutePath());
}
}
return filelist;
}
/**
* 控制生成的日志的个数(如果多于count就删除时间最老的文件)
*
* @param activity
* @param path
* 文件存储在njt目录下路径
* @param name
* 文件的名称标示(用来区分某一类文件的标识)
* @param count
* 最多显示的文件个数
*/
public static void deleteMoreFile(Context activity, String path,
String name, int count) {
String filePath = ""; // 取出农机通存储文件的最外层文件夹
if (path == null || "".equals(path)) {
filePath = getLogPath(activity);
} else {
filePath = getLogPath(activity) + File.separator + path;
}
ArrayList<String> fileList = getFiles(filePath);
if (null != fileList) {
ArrayList<String> fileListNew = new ArrayList<String>();
for (String string : fileList) {
if (string.length()> name.length()&&name.equals(string.substring(0, name.length()))) {
fileListNew.add(string);
}
}
if (null != fileListNew) {
String[] fileArrays = new String[fileListNew.size()];
for (int i = 0; i < fileListNew.size(); i++) {
fileArrays[i] = fileList.get(i);
}
Arrays.sort(fileArrays);
if (fileArrays.length >= count) {
int deleteCount = fileArrays.length - count + 1;
for (int i = 0; i < deleteCount; i++) {
deletefile(filePath, fileArrays[i]);
}
}
}
}
}
/**
* 删除一个路径下名称是fileName的文件
*
* @param path
* @param fileName
*/
public static void deletefile(String path, String fileName) {
try {
File file = new File(path + File.separator + fileName);
file.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 当天开始时间0:00:00:000
*
* @return
*/
public static long getDayStartTime() {
return getDayStartTime(System.currentTimeMillis());
}
/**
* 当天结束时间23:59:59:999
*
* @return
*/
public static long getDayEndTime() {
return getDayStartTime() + ONE_DATE_TIME - 1l;
}
/**
* 当天开始时间0:00:00:000
*
* @return
*/
public static long getDayStartTime(long l) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(l);
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 0, 0, 0);
return c.getTimeInMillis();
}
/**
* 根据年月日的值取得时间片
*
* @param mYear
* @param mMonth 自动减去1900
* @param mDay
* @return long
*/
public static long getTimeStampFromYearMonthDayStart(int mYear, int mMonth,
int mDay) {
Calendar c = Calendar.getInstance();
c.set(mYear, mMonth, mDay, 0, 0, 0);
return c.getTimeInMillis();
}
/**
* 根据年月日的值取得时间片
*
* @param mYear
* @param mMonth 自动减去1900
* @param mDay
* @return long
*/
public static long getTimeStampFromYearMonthDayEnd(int mYear, int mMonth,
int mDay) {
Calendar c = Calendar.getInstance();
c.set(mYear, mMonth, mDay, 59, 59, 59);
return c.getTimeInMillis();
}
}