android 计算所在周所在月的前后日期,跨平台app开发框架首选
作者:互联网
return r + “分钟前”;
}
return “刚刚”;
}
/**
-
将日期以yyyy-MM-dd HH:mm:ss格式化
-
@param dateL
-
日期
-
@return
*/
public static String formatDateTime(long dateL) {
SimpleDateFormat sdf = new SimpleDateFormat(DF_YYYY_MM_DD_HH_MM_SS);
Date date = new Date(dateL);
return sdf.format(date);
}
/**
-
将日期以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;
}
}
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_v
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
ertical_margin"
tools:context=".MainActivity" >
标签:return,String,MM,所在,日期,跨平台,Date,android 来源: https://blog.csdn.net/m0_64604842/article/details/122103695