android中的时间比较大小以及字符串转换为时间

来源:互联网 发布:电脑淘宝精选怎么删除 编辑:程序博客网 时间:2024/05/13 20:17

先上代码

package com.chengxl.timecomparedemo;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.TimeZone;import android.os.Bundle;import android.app.Activity;import android.app.DatePickerDialog;import android.text.TextUtils;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.DatePicker;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {private TextView tvStartTime, tvEndTime;private Button btnStartTime, btnEndTime, submit,before;public String from = "1970-01-01 08:00:00";public String to = "2030-01-01 08:00:00";private Date dateStart, dateEnd;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tvStartTime = (TextView) findViewById(R.id.tv_show_start_time);tvEndTime = (TextView) findViewById(R.id.tv_show_end_time);tvStartTime.setOnClickListener(this);tvEndTime.setOnClickListener(this);submit = (Button) findViewById(R.id.submit);submit.setOnClickListener(this);before = (Button) findViewById(R.id.before);before.setOnClickListener(this);btnStartTime = (Button) findViewById(R.id.btn_show_start_time_select);btnEndTime = (Button) findViewById(R.id.btn_show_end_time_select);btnStartTime.setOnClickListener(this);btnEndTime.setOnClickListener(this);}@Overridepublic void onClick(View v) {if (v == tvStartTime || v == btnStartTime) {showDatePickerDialog(0);} else if (v == tvEndTime || v == btnEndTime) {showDatePickerDialog(1);} else if (v == submit) {checkInput();}else if(v == before){Toast.makeText(MainActivity.this, twoDateDistance(dateStart,dateEnd),Toast.LENGTH_SHORT).show();}}private void showDatePickerDialog(int type) {final Calendar c = Calendar.getInstance();int mYear = c.get(Calendar.YEAR);int mMonth = c.get(Calendar.MONTH);int mDay = c.get(Calendar.DAY_OF_MONTH);if (type == 0) { // start timenew DatePickerDialog(this, mStartDateSetListener, mYear, mMonth,mDay).show();} else if (type == 1) { // end timenew DatePickerDialog(this, mEndDateSetListener, mYear, mMonth, mDay).show();}}private DatePickerDialog.OnDateSetListener mStartDateSetListener = new DatePickerDialog.OnDateSetListener() {public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {Calendar calendar = Calendar.getInstance();calendar.set(year, monthOfYear, dayOfMonth, 0, 0, 0);SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String str = dateFormat.format(calendar.getTime()).toString();from = str;tvStartTime.setText(str.substring(0, 10));}};private DatePickerDialog.OnDateSetListener mEndDateSetListener = new DatePickerDialog.OnDateSetListener() {public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {Calendar calendar = Calendar.getInstance();calendar.set(year, monthOfYear, dayOfMonth, 23, 59, 59);SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String str = dateFormat.format(calendar.getTime()).toString();to = str;tvEndTime.setText(str.substring(0, 10));}};public boolean checkInput() {if (TextUtils.isEmpty(tvStartTime.getText())) {Toast.makeText(MainActivity.this, "请输入开始时间~", Toast.LENGTH_SHORT).show();return false;}if (TextUtils.isEmpty(tvEndTime.getText())) {Toast.makeText(MainActivity.this, "请输入结束时间~", Toast.LENGTH_SHORT).show();return false;}SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");try {dateStart = dateFormat.parse(tvStartTime.getText().toString());dateEnd = dateFormat.parse(tvEndTime.getText().toString());if (dateStart.getTime() > dateEnd.getTime()) {Toast.makeText(MainActivity.this, "开始时间需要小于结束时间哦~~",Toast.LENGTH_SHORT).show();return false;} else if (dateStart.getTime() > new Date().getTime()) {Toast.makeText(MainActivity.this, "开始时间大于现在时间哦~~",Toast.LENGTH_SHORT).show();return false;} else if (dateEnd.getTime() > new Date().getTime()) {Toast.makeText(MainActivity.this, "结束时间大于现在时间哦~~",Toast.LENGTH_SHORT).show();return false;}} catch (ParseException e) {e.printStackTrace();Toast.makeText(MainActivity.this, "数据格式有误!", Toast.LENGTH_SHORT).show();return false;}return true;}public String twoDateDistance(Date startDate, Date endDate) {if (startDate == null || endDate == null) {return null;}long timeLong = endDate.getTime() - startDate.getTime();if (timeLong < 60 * 1000)return timeLong / 1000 + "秒前";else if (timeLong < 60 * 60 * 1000) {timeLong = timeLong / 1000 / 60;return timeLong + "分钟前";} else if (timeLong < 60 * 60 * 24 * 1000) {timeLong = timeLong / 60 / 60 / 1000;return timeLong + "小时前";} else if (timeLong < 60 * 60 * 24 * 1000 * 7) {timeLong = timeLong / 1000 / 60 / 60 / 24;return timeLong + "天前";} else if (timeLong < 60 * 60 * 24 * 1000 * 7 * 4) {timeLong = timeLong / 1000 / 60 / 60 / 24 / 7;return timeLong + "周前";} else {return "就是现在";}}}
以上涉及的内容有时间dialog的显示,以及时间的字符串输出,字符串的时间转换,时间的比较,其中需要注意的一点就是如果只有年月日的时间,时分秒默认为0,这样如果你要跟今天的对比的话有时需要加或减一些东西,这个在具体应用中再具体修改吧。我再用到的时候因为需要今天嘛,但是判断的时候总说我的今天是过时的,然后我就在new Date()后面-1000*60*60*24,这样的话时间就缩到明天了,跟今天的00:00:00比当然是小于的了。最后还写了个方法是判断前一个日期是后一个日期的多久以前的。方法都单独写出来的,我想这都是广大卓友所希望的。另外最需要的图片展示,放心,不会落下的。



0 0
原创粉丝点击