您的位置:首页 > 其它

根据时间戳获取相应的日期和时间

2015-08-06 22:28 399 查看
System.currentTimeMillis()返回的是格林威治时间从1970年1月1日00时00分00秒开始到现在的总毫秒数,(注意是格林威治时间)北京时间是1970年01月01日08时00分00秒;
在我们平时开发时,如果服务器是windows搭建的往往会出现android上的时间和服务器上的时间相差8个小时。
System.currentTimeMillis()也是我们常说的Unix时间戳;
以下是通过时间戳获取日期和时间的一些方法:

public class MyActivity extends Activity {

public static final String TAG = "MyActivity";

private Context mContext;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
Log.d(TAG, "当前时间 时间戳: " + System.currentTimeMillis());
Log.d(TAG, "当前时间 完整时间 : " + timeStampToDate(System.currentTimeMillis()));
Log.d(TAG, "当前时间 year: " + getYearByTimeStamp(System.currentTimeMillis()));
Log.d(TAG, "当前时间 month: " + getMonthByTimeStamp(System.currentTimeMillis()));
Log.d(TAG, "当前时间 day: " + getDayByTimeStamp(System.currentTimeMillis()));
Log.d(TAG, "当前时间 Hour: " + getHourByTimeStamp(System.currentTimeMillis()));
Log.d(TAG, "当前时间 minute: " + getMinuteByTimeStamp(System.currentTimeMillis()));
Log.d(TAG, "当前时间 Second: " + getSecondByTimeStamp(System.currentTimeMillis()));
Log.d(TAG, "两个时间戳的日期是否相等: " + isTwoTimeStampDayEqual(System.currentTimeMillis(), System.currentTimeMillis()));

}

public static String timeStampToDate(long timeStamp){
Date             date = new Date(timeStamp);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = simpleDateFormat.format(date);
return dateStr;
}

public static int getYearByTimeStamp(long timeStamp){
String date = timeStampToDate(timeStamp);
String year = date.substring(0, 4);
return Integer.parseInt(year);
}

public static int getMonthByTimeStamp(long timeStamp){
String date = timeStampToDate(timeStamp);
String month = date.substring(5, 7);
return Integer.parseInt(month);
}

public static int getDayByTimeStamp(long timeStamp){
String date = timeStampToDate(timeStamp);
String day = date.substring(8, 10);
return Integer.parseInt(day);
}

public static int getHourByTimeStamp(long timeStamp){
String date = timeStampToDate(timeStamp);
String hour = date.substring(11, 13);
return Integer.parseInt(hour);
}

public static int getMinuteByTimeStamp(long timeStamp){
String date = timeStampToDate(timeStamp);
String minute = date.substring(14, 16);
return Integer.parseInt(minute);
}

public static int getSecondByTimeStamp(long timeStamp){
String date = timeStampToDate(timeStamp);
String second = date.substring(17, 19);
return Integer.parseInt(second);
}

//判断两个时间戳是否为同一天
public static boolean isTwoTimeStampDayEqual(long firstTimeStamp, long secondTimeStamp){
if(getYearByTimeStamp(firstTimeStamp) == getYearByTimeStamp(secondTimeStamp) &&
getMonthByTimeStamp(firstTimeStamp) == getMonthByTimeStamp(secondTimeStamp)
&& getDayByTimeStamp(firstTimeStamp) == getDayByTimeStamp(secondTimeStamp)){
return true;
}
return false;
}
//将时间字符串转换为Date
public static Date stringToDate(String dateString){
        ParsePosition position = new ParsePosition(0);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date dateValue = simpleDateFormat.parse(dateString, position);
        return dateValue;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: