}
/**
* 将日期以yyyy-MM-dd HH:mm:ss格式化
*
* @param dateL
* 日期
* @return
*/
public static String formatDateTime(long dateL, String formater) {
SimpleDateFormat sdf = new SimpleDateFormat(formater);
return sdf.format(new Date(dateL));
}
/**
* 将日期以yyyy-MM-dd HH:mm:ss格式化
*
* @param dateL
* 日期
* @return
*/
public static String formatDateTime(Date date, String formater) {
SimpleDateFormat sdf = new SimpleDateFormat(formater);
return sdf.format(date);
}
/**
* 将日期字符串转成日期
*
* @param strDate
* 字符串日期
* @return java.util.date日期类型
*/
public static Date parseDate(String strDate) {
DateFormat dateFormat = new SimpleDateFormat(DF_YYYY_MM_DD_HH_MM_SS);
Date returnDate = null;
try {
returnDate = dateFormat.parse(strDate);
} catch (ParseException e) {
Log.v(TAG, "parseDate failed !");
}
return returnDate;
}
/**
* 获取系统当前日期
*
* @return
*/
public static Date gainCurrentDate() {
return new Date();
}
/**
* 验证日期是否比当前日期早
*
* @param target1
* 比较时间1
* @param target2
* 比较时间2
* @return true 则代表target1比target2晚或等于target2,否则比target2早
*/
public static boolean compareDate(Date target1, Date target2) {
boolean flag = false;
try {
String target1DateTime = ToolDateTime.formatDateTime(target1,
DF_YYYY_MM_DD_HH_MM_SS);
String target2DateTime = ToolDateTime.formatDateTime(target2,
DF_YYYY_MM_DD_HH_MM_SS);
if (target1DateTime.compareTo(target2DateTime) <= 0) {
flag = true;
}
} catch (Exception e1) {
System.out.println("比较失败,原因:" + e1.getMessage());
}
return flag;
}
/**
* 对日期进行增加操作
*
* @param target
* 需要进行运算的日期
* @param hour
* 小时
* @return
*/
public static Date addDateTime(Date target, double hour) {
if (null == target || hour < 0) {
return target;
}
return new Date(target.getTime() + (long) (hour * 60 * 60 * 1000));
}
/**
* 对日期进行相减操作
*
* @param target
* 需要进行运算的日期
* @param hour
* 小时
* @return
*/
public static Date subDateTime(Date target, double hour) {
if (null == target || hour < 0) {
return target;
}
return new Date(target.getTime() - (long) (hour * 60 * 60 * 1000));
}
}
MainActivity
package com.yqy.yqy_date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv_week, tv_month;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_week = (TextView) findViewById(R.id.tv_week);
tv_month = (TextView) findViewById(R.id.tv_month);
Date mDate = new Date();
Date sDate = null;
Date eDate = null;
String sstr = "";
String estr = "";
sDate = ToolDateTime.subDateTime(mDate, (mDate.getDay() - 1) * 24);
for (int i = 0; i < 15; i++) {
eDate = ToolDateTime.addDateTime(sDate, 6 * 24);
sstr = ToolDateTime.formatDateTime(sDate,
ToolDateTime.DF_YYYY_MM_DD);
estr = ToolDateTime.formatDateTime(eDate,
ToolDateTime.DF_YYYY_MM_DD);
tv_week.append(sstr + "~" + estr + "\n");
sDate = ToolDateTime.addDateTime(eDate, 24);
}
SimpleDateFormat format = new SimpleDateFormat(ToolDateTime.DF_YYYY_MM_DD);
// 获取当前月第一天:
String first = "";
String last = "";
for (int i = 0; i < 10; i++) {
Calendar c = Calendar.getInstance();
Calendar ca = Calendar.getInstance();
c.add(Calendar.MONTH, i);
c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
first = format.format(c.getTime());
// 获取当前月最后一天
ca.add(Calendar.MONTH, i);
ca.set(Calendar.DAY_OF_MONTH,
ca.getActualMaximum(Calendar.DAY_OF_MONTH));
last = format.format(ca.getTime());
tv_month.append(first+"~"+last+"\n");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
推荐学习资料
-
Android进阶学习全套手册
-
Android对标阿里P7学习视频
-
BAT TMD大厂Android高频面试题
(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
推荐学习资料
-
Android进阶学习全套手册
[外链图片转存中…(img-Ec3ktlF3-1630821475994)]
-
Android对标阿里P7学习视频
[外链图片转存中…(img-NzQ3AvYS-1630821475996)]
-
BAT TMD大厂Android高频面试题
[外链图片转存中…(img-nLQYaMqG-1630821475997)]
CodeChina开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》