Android時間、日付、各種異なる表示のフォーマットツール
8801 ワード
/**
*
*
* :"yyyy-MM-dd DD HH:mm:ss SSS"
* y ,M ,d ,D ,
* H ,m ,s ,S 。 。
*
* * 1:long getTimeLong()
*
*
* * 2:int getTimeInt(String filter)
* , , getTimeInt(“DD”)
*
* * 3:String getTimeString()
* , 2016-11-11 8:20:20
*
* * 4:String getTimeString(long time)
* long , ,
*
* * 5: String getTimeString(long time, String filter)
* long , ,
* getTimeString(1111111L,“MM-dd”) 11111111
*
* * 6:String getTimeString( String filter)
* , , getTimeString(“DD”)
* 2 , , , , 。
* * 7:String getTimeLong( String filter,String date)
* , , getTimeString(“MM-dd”,“8-12”) 8 12
* ,
*/
public class TimeUtil {
/**
* SimpleDateFormat
* G
* y
M
d
h (1~12)
H (0~23)
m
s
S
E
D
F
w
W
a /
k (1~24)
K (0~11)
z
*
*/
/**
*
*/
public static long getTimeLong() {
return System.currentTimeMillis();
}
/**
* , , ,
* int ,
* ,
* getTime(“MM”), 2016 5 20 , 5;
*/
public static int getTimeInt(String filter) {
//
SimpleDateFormat format = new SimpleDateFormat(filter);
String time = format.format(new Date());
return Integer.parseInt(time);
}
/**
*
* String ,
* ,
* getTime(“E”), , “ ”;
*/
public static String getTimeStringE() {
SimpleDateFormat format = new SimpleDateFormat("E");
String time = format.format(new Date());
return time;
}
/**
* , , , , ,
* int ,
* 2016-12-12 14:12:10 ,
* getTime(“2016-12-12 14:12:10”,“HH”); 14
*/
public static int getTimeInt(String StringTime, String filter) {
//
SimpleDateFormat format = new SimpleDateFormat(filter);
String time = format.format(new Date(getTimeLong("yyyy-MM-dd HH:mm:ss", StringTime)));
return Integer.parseInt(time);
}
/**
* ,
* getTimeStringE(“2017-6-26 14:12:10”,“E”); “ ”
*
* @param stringTime
* @return
*/
public static String getTimeStringE(String stringTime) {
SimpleDateFormat format = new SimpleDateFormat("E");
String time = format.format(new Date(getTimeLong("yyyy-MM-dd HH:mm:ss", stringTime)));
return time;
}
/**
*
*/
public static final String getTimeString() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(new Date(getTimeLong()));
}
/**
*
* ,
* , !
*/
public static final String getTimeString(long time) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(new Date(time));
}
/**
*
* ,
* ,getTimeString(1111111111111,"MM-dd");
*/
public static final String getTimeString(long time, String filter) {
SimpleDateFormat format = new SimpleDateFormat(filter);
return format.format(new Date(time));
}
/**
*
* 2016 8 8 12 8 21 ,getTimeString("yyyy-MM-dd"), 2016-8-12
*/
public static final String getTimeString(String filter) {
SimpleDateFormat format = new SimpleDateFormat(filter);
return format.format(new Date(getTimeLong()));
}
/**
* ,
*
* ,
* 2016-6-20 ( ),
* getTimeLong("yyyy-MM-dd","2016-6-20")
* , getTimeLong("MM-dd","6-20") ,
* ,
*/
public static Long getTimeLong(String filter, String date) {
try {
SimpleDateFormat format = new SimpleDateFormat(filter);
Date dateTime = format.parse(date);
return dateTime.getTime();
} catch (Exception e) {
e.printStackTrace();
}
return 0L;
}
/**
*
* :String data= "2016-5-20 12:12:10", :5-20 12:10
* :getTimeLocalString("yyyy-MM-dd HH:mm:ss",data,"MM-dd") , 5-20
* data="2016-5-20", 5-20,
* getTimeLocalString("yyyy-MM-dd ",data,"MM-dd")!
*/
public static String getTimeLocalString(String filter, String data, String filterInside) {
Long timeLong = getTimeLong(filter, data);
return getTimeString(timeLong, filterInside);
}
}