import
android
.
content
.
Context
;
import
android
.
text
.
TextUtils
;
import
android
.
text
.
format
.
DateFormat
;
import
com
.
peopleapp
.
en
.
R
;
import
java
.
text
.
ParseException
;
import
java
.
text
.
SimpleDateFormat
;
import
java
.
util
.
Date
;
import
java
.
util
.
Locale
;
import
java
.
util
.
TimeZone
;
public
class
TimeUtil
{
private
static
final
int
seconds_of_1minute
=
60
;
private
static
final
int
seconds_of_1hour
=
60
*
60
;
private
static
final
int
seconds_of_2hour
=
2
*
60
*
60
;
private
static
final
int
seconds_of_3hour
=
3
*
60
*
60
;
private
static
final
String YMDHMS_FORMAT
=
"yyyy-MM-dd HH:mm:ss"
;
private
static
final
String search_DateFormat
=
"MM/dd/yyyy HH:mm:ss"
;
private
static
final
String TIME_ZERO
=
"00:00"
;
private
static
final
String TIME_MAX
=
"23:59:59"
;
public
static
Date
stringConvertDate
(
String time
)
{
SimpleDateFormat sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm"
,
Locale
.
CHINA
)
;
Date data
=
null
;
try
{
data
=
sdf
.
parse
(
time
)
;
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
(
)
;
return
data
;
public
static
String
timeAgo
(
Context context
,
long
createdTime
)
{
return
timeAgo
(
context
,
new
Date
(
createdTime
)
)
;
public
static
String
timeAgo
(
Context context
,
Date createdTime
)
{
SimpleDateFormat format
=
new
SimpleDateFormat
(
"MM-dd HH:mm"
,
Locale
.
CHINA
)
;
if
(
createdTime
!=
null
)
{
long
agoTimeInMin
=
(
new
Date
(
System
.
currentTimeMillis
(
)
)
.
getTime
(
)
-
createdTime
.
getTime
(
)
)
/
1000
/
60
;
if
(
agoTimeInMin
<=
1
)
{
return
context
.
getString
(
R
.
string
.
just_now
)
;
}
else
if
(
agoTimeInMin
<=
60
)
{
return
agoTimeInMin
+
context
.
getString
(
R
.
string
.
mins_ago
)
;
}
else
if
(
agoTimeInMin
<=
60
*
24
)
{
return
agoTimeInMin
/
60
+
context
.
getString
(
R
.
string
.
hours_ago
)
;
}
else
if
(
agoTimeInMin
<=
60
*
24
*
2
)
{
return
agoTimeInMin
/
(
60
*
24
)
+
context
.
getString
(
R
.
string
.
days_ago
)
;
}
else
{
return
format
.
format
(
createdTime
)
;
}
else
{
return
format
.
format
(
new
Date
(
0
)
)
;
public
static
String
getDateTimeAgo
(
Context context
,
long
timeStamp
)
{
return
timeAgo
(
context
,
new
Date
(
timeStamp
)
)
;
public
static
String
getUSDateTimeFormat
(
long
timeStamp
)
{
SimpleDateFormat usSdf
=
new
SimpleDateFormat
(
"HH:mm, MMMM dd, yyyy"
,
Locale
.
US
)
;
return
usSdf
.
format
(
new
Date
(
timeStamp
)
)
;
public
static
String
getCurrentTimeStamp
(
)
{
return
String
.
valueOf
(
System
.
currentTimeMillis
(
)
/
1000
)
;
* local ---> UTC
* @return
public
static
String
Local2UTC
(
)
{
SimpleDateFormat sdf
=
new
SimpleDateFormat
(
YMDHMS_FORMAT
)
;
sdf
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"UTC"
)
)
;
String gmtTime
=
sdf
.
format
(
new
Date
(
)
)
;
return
gmtTime
;
* UTC --->local
* @param utcTime UTC
* @return
public
static
String
utc2Local
(
String utcTime
)
{
try
{
if
(
TextUtils
.
isEmpty
(
utcTime
)
)
{
return
""
;
SimpleDateFormat utcFormater
=
new
SimpleDateFormat
(
YMDHMS_FORMAT
)
;
utcFormater
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"UTC"
)
)
;
Date gpsUTCDate
=
null
;
try
{
gpsUTCDate
=
utcFormater
.
parse
(
utcTime
)
;
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
(
)
;
SimpleDateFormat localFormater
=
new
SimpleDateFormat
(
YMDHMS_FORMAT
)
;
localFormater
.
setTimeZone
(
TimeZone
.
getDefault
(
)
)
;
String localTime
=
localFormater
.
format
(
gpsUTCDate
.
getTime
(
)
)
;
return
localTime
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
(
)
;
return
""
;
* @param tTime
* @return
public
static
String
getTimeRange
(
Context context
,
String tTime
)
{
String mTime
=
""
;
try
{
mTime
=
utc2Local
(
tTime
)
;
if
(
TextUtils
.
isEmpty
(
mTime
)
)
{
return
""
;
SimpleDateFormat sdf
=
new
SimpleDateFormat
(
YMDHMS_FORMAT
)
;
sdf
.
setTimeZone
(
TimeZone
.
getDefault
(
)
)
;
Date curDate
=
new
Date
(
System
.
currentTimeMillis
(
)
)
;
String dataStrNew
=
sdf
.
format
(
curDate
)
;
Date startTime
=
null
;
try
{
curDate
=
sdf
.
parse
(
dataStrNew
)
;
startTime
=
sdf
.
parse
(
mTime
)
;
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
(
)
;
long
between
=
(
curDate
.
getTime
(
)
-
startTime
.
getTime
(
)
)
/
1000
;
int
elapsedTime
=
(
int
)
(
between
)
;
if
(
elapsedTime
<
0
)
{
return
context
.
getResources
(
)
.
getString
(
R
.
string
.
timeutils_default_oneminageo
)
;
if
(
elapsedTime
<
seconds_of_1minute
)
{
return
context
.
getResources
(
)
.
getString
(
R
.
string
.
timeutils_default_oneminageo
)
;
if
(
elapsedTime
<
seconds_of_1hour
)
{
return
elapsedTime
/
seconds_of_1minute
+
" "
+
context
.
getResources
(
)
.
getString
(
R
.
string
.
timeutils_default_moreminsageo
)
;
if
(
elapsedTime
<
seconds_of_2hour
)
{
return
context
.
getResources
(
)
.
getString
(
R
.
string
.
timeutils_default_onehourageo
)
;
if
(
elapsedTime
<
seconds_of_3hour
)
{
return
elapsedTime
/
seconds_of_1hour
+
" "
+
context
.
getResources
(
)
.
getString
(
R
.
string
.
timeutils_default_morehoursageo
)
;
return
""
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
(
)
;
return
context
.
getResources
(
)
.
getString
(
R
.
string
.
timeutils_default_threehoursageo
)
;
public
static
String
getTimeRange
(
Context context
,
long
time
)
{
long
between
=
(
System
.
currentTimeMillis
(
)
-
time
)
/
1000
;
int
elapsedTime
=
(
int
)
(
between
)
;
if
(
elapsedTime
<
0
)
{
return
context
.
getResources
(
)
.
getString
(
R
.
string
.
timeutils_default_oneminageo
)
;
if
(
elapsedTime
<
seconds_of_1minute
)
{
return
context
.
getResources
(
)
.
getString
(
R
.
string
.
timeutils_default_oneminageo
)
;
if
(
elapsedTime
<
seconds_of_1hour
)
{
return
elapsedTime
/
seconds_of_1minute
+
" "
+
context
.
getResources
(
)
.
getString
(
R
.
string
.
timeutils_default_moreminsageo
)
;
if
(
elapsedTime
<
seconds_of_2hour
)
{
return
context
.
getResources
(
)
.
getString
(
R
.
string
.
timeutils_default_onehourageo
)
;
if
(
elapsedTime
<
seconds_of_3hour
)
{
return
elapsedTime
/
seconds_of_1hour
+
" "
+
context
.
getResources
(
)
.
getString
(
R
.
string
.
timeutils_default_morehoursageo
)
;
return
""
;
* 时间戳转换成日期格式字符串
* @return
public
static
String
timeStamp2Date
(
long
seconds
,
String format
)
{
if
(
format
==
null
||
format
.
isEmpty
(
)
)
{
format
=
"yyyy-MM-dd HH:mm:ss"
;
SimpleDateFormat sdf
=
new
SimpleDateFormat
(
format
)
;
return
sdf
.
format
(
new
Date
(
seconds
)
)
;
public
static
String
longToString
(
long
longNum
,
String dateFormat
)
{
if
(
TextUtils
.
isEmpty
(
dateFormat
)
)
{
dateFormat
=
YMDHMS_FORMAT
;
SimpleDateFormat format
=
new
SimpleDateFormat
(
dateFormat
)
;
Date date
=
new
Date
(
longNum
)
;
return
format
.
format
(
date
)
;
public
static
String
secondsToTime
(
int
time
)
{
String timeStr
=
null
;
int
hour
=
0
;
int
minute
=
0
;
int
second
=
0
;
if
(
time
<=
0
)
return
TIME_ZERO
;
else
{
minute
=
time
/
60
;
if
(
minute
<
60
)
{
second
=
time
%
60
;
timeStr
=
unitFormat
(
minute
)
+
":"
+
unitFormat
(
second
)
;
}
else
{
hour
=
minute
/
60
;
if
(
hour
>
23
)
return
TIME_MAX
;
minute
=
minute
%
60
;
second
=
time
-
hour
*
3600
-
minute
*
60
;
timeStr
=
unitFormat
(
hour
)
+
":"
+
unitFormat
(
minute
)
+
":"
+
unitFormat
(
second
)
;
return
timeStr
;
public
static
String
unitFormat
(
int
i
)
{
String retStr
=
null
;
try
{
if
(
i
>=
0
&&
i
<
10
)
retStr
=
"0"
+
Integer
.
toString
(
i
)
;
retStr
=
""
+
i
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
(
)
;
return
retStr
;
public
static
long
searchTimeToLong
(
String time
)
{
if
(
TextUtils
.
isEmpty
(
time
)
)
{
return
0
L
;
try
{
String
[
]
split
=
time
.
split
(
" "
)
;
String tempTime
=
split
[
0
]
+
" "
+
split
[
1
]
;
int
diff
=
0
;
if
(
"pm"
.
equals
(
split
[
2
]
)
)
{
diff
=
1000
*
12
*
60
*
60
;
SimpleDateFormat sdf
=
new
SimpleDateFormat
(
search_DateFormat
)
;
sdf
.
setTimeZone
(
TimeZone
.
getDefault
(
)
)
;
Date startTime
=
null
;
startTime
=
sdf
.
parse
(
tempTime
)
;
return
(
startTime
.
getTime
(
)
+
diff
)
;
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
(
)
;
return
0
L
;
public
static
String
searchTimeFormat
(
String time
)
{
if
(
TextUtils
.
isEmpty
(
time
)
)
{
return
""
;
try
{
String date
=
(
String
)
DateFormat
.
format
(
"yyyy-MM-dd HH:mm:ss"
,
searchTimeToLong
(
time
)
)
;
return
date
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
(
)
;
return
""
;
package com.peopleapp.en.util;import android.content.Context;import android.text.TextUtils;import android.text.format.DateFormat;import com.peopleapp.en.R;import java.text.ParseException;im...
注:
转
发请注明原地址:https://www.niwoxuexi.com/blog/
android
/article/170...
在
Android
开发过程中,经常会遇到日期的各种格式
转
换,主要使用SimpleDateFormat这个类来实现,掌握了这个类,可以
转
换任何你想要的各种格式。
常见的日期格式:
1,日期格式:String dateString = "2017-06-20
public static long getGmtTimeZone() {
long _t = TimeZone.getDefault().getOffset(System.currentTimeMillis()) / (3600 * 1000);
return _t;
UTC
毫秒
转
时间
日期格式
public static String longtransformstring(Long time) {
Simple..
public static String Local2
UTC
(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.get
public static String get
UTC
FromLocalTime_(int hour, int minute, int second) {
StringBuffer
UTC
TimeBuffer = new StringBuffer();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:...
在有些软件中,可能需要展示一些
时间
信息,而这些信息可能是Server以
UTC
格式或Unix timestamp 格式推送过来的,终端可能需要将这些
转
换为本地
时间
展示。
终端的制式可能是12小时制、也可能是24小时制的
今天就遇到将
utc
时间
格式
转
换为GMT
HH:返回的是24小时制的
时间
hh:返回的是12小时制的
时间
long
utc
1=Long.parseLong(
utc
);
long
utc
2=1400000000L;//自己的偏移量
long
utc
z=(
utc
1+
utc
2)*1000;
public static String getFormatedDa
String
转
timestamp是一种常见的需求,我们可以写一个
工具
类来实现这个功能。具体步骤如下:
1. 声明一个静态方法,命名为stringToTimestamp,输入参数为一个String类型的日期
时间
字符串,输出结果为一个Timestamp类型的日期
时间
。
2. 首先,需要将String类型的日期
时间
解析为java.util.Date类型的日期
时间
。我们可以使用SimpleDateFormat类中的parse方法进行解析。在调用parse方法时需要注意两点:一是要指定日期
时间
字符串的格式,二是要处理解析异常。
3. 然后,需要将java.util.Date类型的日期
时间
转
换成java.sql.Timestamp类型的日期
时间
。我们可以使用Timestamp类的valueOf方法将Date类型
转
换成Timestamp类型。
以下是该
工具
类的示例代码:
public class DateUtil {
//将String类型的日期
时间
转
换成Timestamp类型的日期
时间
public static Timestamp stringToTimestamp(String dateStr) {
Date date = null;
Timestamp timestamp = null;
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = dateFormat.parse(dateStr);
timestamp = new Timestamp(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
return timestamp;
使用该
工具
类的方式如下:
String dateStr = "2022-01-01 01:01:01";
Timestamp timestamp = DateUtil.stringToTimestamp(dateStr);
以上就是一个简单的string
转
timestamp
工具
类的实现。