- 浏览: 39547 次
- 性别:
- 来自: 深圳
文章分类
最新评论
//根据输入的日期(MM/dd/yyyy)返回最近7天的日期
public static List<String> getNextSevenDate(String dateStr,String dateFormat)
{
List<String> dateList=new ArrayList<String>();
Date date=new Date(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);
dateList.add(dateStr);
for(int i=0;i<6;i++)
{
calendar.add(Calendar.DATE,1);
dateList.add(sdf.format(calendar.getTime()));
}
return dateList;
}
SimpleDateFormat sdf = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
这一行最重要,它确立了转换的格式,yyyy是完整的公元年,MM是月份,dd是日期,至于HH:mm:ss就不需要我再解释了吧!
ps:为什么有的格式大写,有的格式小写,那是怕避免混淆,例如MM是月份,mm是分;HH是24小时制,而hh是12小时制
1.字符串转日期:
2002-10-8 15:30:22要把它转成日期,可以用
Date date = sdf.parse( " 2002-10-8 15:30:22 " );
2.日期转字符串
假如把今天的日期转成字符串可用
String datestr = sdf.format( new Date());
这个字符串的内容便类似2002-10-08 14:55:38
透过这个API我们便可以随心所欲的将日期转成我们想要的字符串格式,例如希望将日期输出成2002年10月08日,
我们可以这么写:
SimpleDateFormat sdf = new SimpleDateFormat( " yyyy年MM月dd日 " );
String datestr = sdf.format( new Date());
//随机数
//生成 [0-n) 个不重复的随机数
//list 用来保存这些随机数
ArrayList list = new ArrayList();
int n = 10;
Random rand = new Random();
boolean[] bool = new boolean[n];
int num =0;
for (int i = 0; i<n; i++){
do{
//如果产生的数相同继续循环
num = rand.nextInt(n);
}while(bool[num]);
bool[num] =true;
list.add(num);
}
System.out.println (list);
}
获取当前时间:
1.
String[] weekDayArray={"日","一","二","三","四","五","六"};
Calendar calendar=Calendar.getInstance();
System.out.println(calendar.getTime());
int year=calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH);
int day=calendar.get(Calendar.DATE);
int weekDay=calendar.get((Calendar.DAY_OF_WEEK));
int am_pm=calendar.get(Calendar.AM_PM);
int hour=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
int second=calendar.get(Calendar.SECOND);
String dateStr=year+"年"+(month+1)+"月"+day+"日"+" 星期"+weekDayArray[weekDay-1];
if (am_pm==Calendar.AM){dateStr+=" 上午";
} else{ dateStr+=" 下午";
}
dateStr+=hour+"时"+minute+"分"+second+"秒";
System.out.println("dd"+dateStr);
2.
Date d=new Date();
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyyMMddHHmmss");
System.out.println(sdf.format(d));
public static final String FormatDayToChinaTime24H = "yyyy年MM月dd日";
/**
* 将yyyy-mm-dd转为yyyy-m-d
*
* @param day
* 距离现在之后的天数
* @return Date:距离现在之后的若干天的日期;
*/
public static String getYMDDays(String day){
String dayTime=day;
if(String.valueOf(day.charAt(8)).equals("0"))dayTime=day.substring(0,8)+day.substring(9);
if(String.valueOf(day.charAt(5)).equals("0"))dayTime=dayTime.substring(0,5)+dayTime.substring(6);
return dayTime;
}
/**
* 两个日期间的天数
*
* @param days
* 距离现在之后的天数
* @return Date:距离现在之后的若干天的日期;
*/
public static int getIntervalDays(Date startday,Date endday){
if(startday.after(endday)){
Date cal=startday;
startday=endday;
endday=cal;
}
long sl=startday.getTime();
long el=endday.getTime();
long ei=el-sl;
return (int)(ei/(1000*60*60*24));
}
/**
* 取得距离现在多少天(距离现在之后的若干天)
*
* @param days
* 距离现在之后的天数
* @return Date:距离现在之后的若干天的日期;
*/
public static Date getDate(int days) {
Date dateresult = new Date();
try {
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
// Create our Gregorian Calendar.
GregorianCalendar cal = new GregorianCalendar();
// Set the date and time of our calendar
// to the system&s date and time
cal.setTime(new Date());
cal.add(GregorianCalendar.DAY_OF_MONTH, days);
dateresult = cal.getTime();
} catch (Exception e) {
System.out.println("exception" + e.toString());
}
return dateresult;
}
/**
* @根据当前日期计算n天后的日期
* @return String
*/
public static Date afterNDay(Date date, int n) {
Calendar c = Calendar.getInstance();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
c.setTime(date);
c.add(Calendar.DATE, n);
Date d2 = c.getTime();
// String s=df.format(d2);
return d2;
}
/**
* @根据当前日期计算n天后的日期
* @return String
*/
public static String afterDay(Date date, int n) {
Calendar c = Calendar.getInstance();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
// String s=df.format(d2);
return df.toString();
}
/**
* @获取当前时间是星期几,“星期日”
* @return String
*/
public static String getDayInWeek() {
Date today = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E");
return simpleDateFormat.format(today);
}
/**
*
* @跟具字符串时间来获得星期几,(SimpleDateFormat("yyyy-MM-dd"))
* @return
*/
public static String getDayInWeek(String mydatestring) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
String showDate ="";
try {
date = dateFormat.parse(mydatestring);
} catch (ParseException e) {
}
Calendar cd = Calendar.getInstance();
cd.setTime(date);
int mydate = cd.get(Calendar.DAY_OF_WEEK);
switch (mydate) {
case 1:
showDate = "星期日";
break;
case 2:
showDate = "星期一";
break;
case 3:
showDate = "星期二";
break;
case 4:
showDate = "星期三";
break;
case 5:
showDate = "星期四";
break;
case 6:
showDate = "星期五";
break;
default:
showDate = "星期六";
break;
}
return showDate;
}
/**
* @根据日期时间,获取当前时间是星期几,“星期日”
* @return String
*/
public static String getDayInWeek(Date date) {
if (date == null)
return "";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E");
return simpleDateFormat.format(date);
}
/**
* @获取当前时间在这个月的天
* @return String
*/
public static String getDayInMonth() {
Date today = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d");
return simpleDateFormat.format(today);
}
/**
* @根据日期,获取当前时间在这个月的天
* @return String
*/
public static String getDayInMonth(Date date) {
if (date == null)
return "";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d");
return simpleDateFormat.format(date);
}
/**
* @根据在本年当中,获取月份格式"M"
* @return String
*/
public static String getMonthInYear() {
Date today = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("M");
return simpleDateFormat.format(today);
}
/**
* @根据在本年当中,获取月份格式"MM"
* @return String
*/
public static String getMonthInYear2() {
java.util.Date today = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
return simpleDateFormat.format(today);
}
/**
* @根据日期,获取月份格式"MM"
* @return String
*/
public static String getMonthInYear2(String formatDate) {
java.util.Date day = getDate(formatDate, "yyyy-MM-dd");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
return simpleDateFormat.format(day);
}
/**
* @获取当前时间的年,格式"yyyy"
* @return String
*/
public static String getYear() {
java.util.Date today = new java.util.Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
return simpleDateFormat.format(today);
}
/**
* @根据时间,获取年,格式"yyyy"
* @return String
*/
public static String getYear(String formatDate) {
java.util.Date day = getDate(formatDate, "yyyy-MM-dd");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
return simpleDateFormat.format(day);
}
/**
* @根据日期以及日期格式,获取日期字符串表达
* @return String
*/
public static String getFormatDate(java.util.Date thisDate, String format) {
if (thisDate == null)
return "";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.format(thisDate);
}
/**
* @根据日期以及日期格式,获取日期
* @return String
*/
public static java.util.Date getDate(String date, String format) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
try {
return simpleDateFormat.parse(date);
} catch (ParseException ex) {
return null;
}
}
/**
* @返回系统当前月的第一天所表示的日期
* @param monthDate
* Date
* @return Date
*/
public static java.util.Date getFirstDateMonth() {
try {
String date_s = getYear() + "-" + getMonthInYear2() + "-01";
return getDate(date_s, "yyyy-MM-dd");
} catch (Exception er) {
er.printStackTrace();
}
return null;
}
/**
* @返回系统当前月的第一天所表示的日期
* @param monthDate
* Date
* @return Date
*/
public static java.util.Date getFirstDateMonth(String formatDate) {
try {
String date_s = getYear(formatDate) + "-"
+ getMonthInYear2(formatDate) + "-01";
return getDate(date_s, "yyyy-MM-dd");
} catch (Exception er) {
er.printStackTrace();
}
return null;
}
/**
* @返回系统当前月的第一天所表示的日期
* @param monthDate
* Date
* @return Date
*/
public static String getFirstDateMonthToString() {
try {
return getYear() + "-" + getMonthInYear2() + "-01";
} catch (Exception er) {
er.printStackTrace();
}
return null;
}
/**
* @返回系统当前月的第一天所表示的日期
* @param monthDate
* Date
* @return Date
*/
public static String getFirstDateMonthToString(String formatDate) {
try {
return getYear(formatDate) + "-" + getMonthInYear2(formatDate)
+ "-01";
} catch (Exception er) {
er.printStackTrace();
}
return null;
}
/**
* @返回系统当前月的最后一天所表示的日期
* @return Date
*/
public static java.util.Date getLastDateMonth() {
try {
int lastday = getDaySumInTheMonth();
String date_s;
if (lastday < 10)
date_s = getYear() + "-" + getMonthInYear2() + "-0" + lastday;
else
date_s = getYear() + "-" + getMonthInYear2() + "-" + lastday;
return getDate(date_s, "yyyy-MM-d");
} catch (Exception er) {
er.printStackTrace();
}
return null;
}
/**
* @返回系统当前月的最后一天所表示的日期
* @return Date
*/
public static String getLastDateMonthToString(String formatDate) {
try {
int lastday = getDaySumInTheMonth(formatDate);
if (lastday < 10)
return getYear(formatDate) + "-" + getMonthInYear2(formatDate)
+ "-0" + lastday;
else
return getYear(formatDate) + "-" + getMonthInYear2(formatDate)
+ "-" + lastday;
} catch (Exception er) {
er.printStackTrace();
}
return null;
}
/**
* @根据时间,返回最后一天所表示的日期
* @param formatDate
* String
* @return Date
*/
public static java.util.Date getLastDateMonth(String formatDate) {
try {
int lastday = getDaySumInTheMonth(formatDate);
String month = getMonthInYear2(formatDate);
String year = getYear(formatDate);
String date_s;
if (lastday < 10)
date_s = year + "-" + month + "-0" + lastday;
else
date_s = year + "-" + month + "-" + lastday;
System.out.println(date_s);
return getDate(date_s, "yyyy-MM-d");
} catch (Exception er) {
er.printStackTrace();
}
return null;
}
/**
* @获得这个月的天数
* @return int
*/
public static int getDaySumInTheMonth() {
String month = DayFormat.getMonthInYear();
if (month.equalsIgnoreCase("12"))
return 31;
int mon = Integer.parseInt(month);
java.util.Date date = DayFormat.getDate(DayFormat.getYear() + "-"
+ (mon + 1) + "-" + "1", "yyyy-MM-d");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, -1);
return Integer.parseInt(new SimpleDateFormat("dd")
.format(cal.getTime()));
}
/**
* @获得所在日期月份的天数
* @return int
*/
public static int getDaySumInTheMonth(java.util.Date date) {
String month = new SimpleDateFormat("MM").format(date);
if (month.equalsIgnoreCase("12"))
return 31;
int mon = Integer.parseInt(month);
java.util.Date date2 = DayFormat.getDate(new SimpleDateFormat("yyyy")
.format(date)
+ "-" + (mon + 1) + "-" + "1", "yyyy-MM-d");
Calendar cal = Calendar.getInstance();
cal.setTime(date2);
cal.add(Calendar.DATE, -1);
return Integer.parseInt(new SimpleDateFormat("dd")
.format(cal.getTime()));
}
/**
* @获得所在日期月份的天数
* @return int
*/
public static int getDaySumInTheMonth(String formatDate) {
String month = DayFormat.getMonthInYear2(formatDate);
if (month.equalsIgnoreCase("12"))
return 31;
int mon = Integer.parseInt(month);
java.util.Date date = DayFormat.getDate(DayFormat.getYear(formatDate)
+ "-" + (mon + 1) + "-" + "1", "yyyy-MM-d");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, -1);
return Integer.parseInt(new SimpleDateFormat("dd")
.format(cal.getTime()));
}
/**
* @把日期格式转换成字符串格式,格式为'yyyy-MM-dd'
* @param date
* Date
* @return String
*/
public static String getFormatDate(java.util.Date date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return simpleDateFormat.format(date);
}
public final static String FormatDay1 = "yyyy-MM-dd";
public final static String FormatDay2 = "yyyy年MM月dd日";
public final static String FormatDay3 = "yyyy年MM月dd日 HH:mm:ss";
public final static String FormatDay4 = "yyyy-MM-dd HH:mm";
public static final String Format24Datetime1 = "yyyy-MM-dd HH:mm:ss";// 24
public static final String Format12Datetime1 = "yyyy-MM-dd hh:mm:ss";// 12
/**
* 获取当前时间
*
* @return
*/
public static String getToday() {
return getToday(FormatDay1);
}
public static java.sql.Timestamp getCurrentTimestamp() {
return new Timestamp(System.currentTimeMillis());
}
public static java.util.Date getCurrentUtilDate() {
return new java.util.Date(System.currentTimeMillis());
}
public static String getToday(String formatDay) {
java.util.Date today = new java.util.Date();
SimpleDateFormat df = new SimpleDateFormat(formatDay);
return df.format(today).toString();
}
public static String get24DateTime() {
return get24DateTime(Format24Datetime1);
}
public static String get24DateTime(String format24DateTime) {
java.util.Date today = new java.util.Date();
SimpleDateFormat df = new SimpleDateFormat(format24DateTime);
return df.format(today).toString();
}
public static String get12DateTime() {
return get12DateTime(Format12Datetime1);
}
public static String get12DateTime(String format12DateTime) {
java.util.Date today = new java.util.Date();
SimpleDateFormat df = new SimpleDateFormat(format12DateTime);
return df.format(today).toString();
}
public static String get24DateTime(Timestamp timestamp) {
SimpleDateFormat df = new SimpleDateFormat(Format24Datetime1);
return df.format(timestamp).toString();
}
public static String get12DateTime(Timestamp timestamp) {
SimpleDateFormat df = new SimpleDateFormat(Format12Datetime1);
return df.format(timestamp).toString();
}
// STRING到日期
public static java.sql.Date stringToDate(String dateStr) {
return java.sql.Date.valueOf(dateStr);
}
public static java.sql.Timestamp stringToTimestamp(String timestampStr) {
if (timestampStr == null || timestampStr.length() < 1)
return null;
return java.sql.Timestamp.valueOf(timestampStr);
}
public static java.sql.Timestamp stringToTimestamp2(String dateStr) {
if (dateStr == null || dateStr.length() < 1)
return null;
return java.sql.Timestamp.valueOf(dateStr + " 00:00:00.000000000");
}
public static java.sql.Timestamp stringToTimestamp3(String dateStr) {
if (dateStr == null || dateStr.length() < 1)
return null;
return java.sql.Timestamp.valueOf(dateStr + ":00.000000000");
}
public static java.sql.Time stringToTime(String timeStr) {
return java.sql.Time.valueOf(timeStr);
}
// 日期到STRING
public static String dateToString(java.sql.Date datee) {
return datee.toString();
}
public static String timestampToString(java.sql.Timestamp timestampe) {
return timestampe.toString();
}
public static String timestampToStringForFormat(Timestamp timestamp,
String format) {
if (timestamp == null)
return "";
SimpleDateFormat df = new SimpleDateFormat(format);
return df.format(timestamp).toString();
}
public static String getTimestampToDateTime15Len(
java.sql.Timestamp timestampe) {
if (timestampe == null || timestampe.toString().length() < 1)
return "";
return timestampe.toString().substring(0, 16);
}
public static String timeToString(java.sql.Time timee) {
return timee.toString();
}
public static java.sql.Timestamp StringToTimestamp(String dateString)
throws ParseException {
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
FormatDay1);
formatter.setLenient(false);
return new java.sql.Timestamp((formatter.parse(dateString).getTime()));
}
public static java.sql.Timestamp StringToTimestampAll(String dateString)
throws ParseException {
return java.sql.Timestamp.valueOf(dateString);
}
//获得周日的日期
public static String getSunday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
//获得周一的日期
public static String getMonday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
//获得周二的日期
public static String getTuesday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.TUESDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
//获得周三的日期
public static String getWednesday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.WEDNESDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
//获得周四的日期
public static String getThursday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.THURSDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
//获得周五的日期
public static String getFriday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
//获得周六的日期
public static String getSaturday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.SATURDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
public static void main(String args[]) {
try {
String d = "2007-05-15 19:23:48.703";
System.out.println(d);
java.sql.Timestamp timestampe = java.sql.Timestamp.valueOf(d);
System.out.println(timestampe.toString());
} catch (Exception er) {
er.printStackTrace();
}
}
public static List<String> getNextSevenDate(String dateStr,String dateFormat)
{
List<String> dateList=new ArrayList<String>();
Date date=new Date(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);
dateList.add(dateStr);
for(int i=0;i<6;i++)
{
calendar.add(Calendar.DATE,1);
dateList.add(sdf.format(calendar.getTime()));
}
return dateList;
}
SimpleDateFormat sdf = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
这一行最重要,它确立了转换的格式,yyyy是完整的公元年,MM是月份,dd是日期,至于HH:mm:ss就不需要我再解释了吧!
ps:为什么有的格式大写,有的格式小写,那是怕避免混淆,例如MM是月份,mm是分;HH是24小时制,而hh是12小时制
1.字符串转日期:
2002-10-8 15:30:22要把它转成日期,可以用
Date date = sdf.parse( " 2002-10-8 15:30:22 " );
2.日期转字符串
假如把今天的日期转成字符串可用
String datestr = sdf.format( new Date());
这个字符串的内容便类似2002-10-08 14:55:38
透过这个API我们便可以随心所欲的将日期转成我们想要的字符串格式,例如希望将日期输出成2002年10月08日,
我们可以这么写:
SimpleDateFormat sdf = new SimpleDateFormat( " yyyy年MM月dd日 " );
String datestr = sdf.format( new Date());
//随机数
//生成 [0-n) 个不重复的随机数
//list 用来保存这些随机数
ArrayList list = new ArrayList();
int n = 10;
Random rand = new Random();
boolean[] bool = new boolean[n];
int num =0;
for (int i = 0; i<n; i++){
do{
//如果产生的数相同继续循环
num = rand.nextInt(n);
}while(bool[num]);
bool[num] =true;
list.add(num);
}
System.out.println (list);
}
获取当前时间:
1.
String[] weekDayArray={"日","一","二","三","四","五","六"};
Calendar calendar=Calendar.getInstance();
System.out.println(calendar.getTime());
int year=calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH);
int day=calendar.get(Calendar.DATE);
int weekDay=calendar.get((Calendar.DAY_OF_WEEK));
int am_pm=calendar.get(Calendar.AM_PM);
int hour=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
int second=calendar.get(Calendar.SECOND);
String dateStr=year+"年"+(month+1)+"月"+day+"日"+" 星期"+weekDayArray[weekDay-1];
if (am_pm==Calendar.AM){dateStr+=" 上午";
} else{ dateStr+=" 下午";
}
dateStr+=hour+"时"+minute+"分"+second+"秒";
System.out.println("dd"+dateStr);
2.
Date d=new Date();
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyyMMddHHmmss");
System.out.println(sdf.format(d));
public static final String FormatDayToChinaTime24H = "yyyy年MM月dd日";
/**
* 将yyyy-mm-dd转为yyyy-m-d
*
* @param day
* 距离现在之后的天数
* @return Date:距离现在之后的若干天的日期;
*/
public static String getYMDDays(String day){
String dayTime=day;
if(String.valueOf(day.charAt(8)).equals("0"))dayTime=day.substring(0,8)+day.substring(9);
if(String.valueOf(day.charAt(5)).equals("0"))dayTime=dayTime.substring(0,5)+dayTime.substring(6);
return dayTime;
}
/**
* 两个日期间的天数
*
* @param days
* 距离现在之后的天数
* @return Date:距离现在之后的若干天的日期;
*/
public static int getIntervalDays(Date startday,Date endday){
if(startday.after(endday)){
Date cal=startday;
startday=endday;
endday=cal;
}
long sl=startday.getTime();
long el=endday.getTime();
long ei=el-sl;
return (int)(ei/(1000*60*60*24));
}
/**
* 取得距离现在多少天(距离现在之后的若干天)
*
* @param days
* 距离现在之后的天数
* @return Date:距离现在之后的若干天的日期;
*/
public static Date getDate(int days) {
Date dateresult = new Date();
try {
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
// Create our Gregorian Calendar.
GregorianCalendar cal = new GregorianCalendar();
// Set the date and time of our calendar
// to the system&s date and time
cal.setTime(new Date());
cal.add(GregorianCalendar.DAY_OF_MONTH, days);
dateresult = cal.getTime();
} catch (Exception e) {
System.out.println("exception" + e.toString());
}
return dateresult;
}
/**
* @根据当前日期计算n天后的日期
* @return String
*/
public static Date afterNDay(Date date, int n) {
Calendar c = Calendar.getInstance();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
c.setTime(date);
c.add(Calendar.DATE, n);
Date d2 = c.getTime();
// String s=df.format(d2);
return d2;
}
/**
* @根据当前日期计算n天后的日期
* @return String
*/
public static String afterDay(Date date, int n) {
Calendar c = Calendar.getInstance();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
// String s=df.format(d2);
return df.toString();
}
/**
* @获取当前时间是星期几,“星期日”
* @return String
*/
public static String getDayInWeek() {
Date today = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E");
return simpleDateFormat.format(today);
}
/**
*
* @跟具字符串时间来获得星期几,(SimpleDateFormat("yyyy-MM-dd"))
* @return
*/
public static String getDayInWeek(String mydatestring) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
String showDate ="";
try {
date = dateFormat.parse(mydatestring);
} catch (ParseException e) {
}
Calendar cd = Calendar.getInstance();
cd.setTime(date);
int mydate = cd.get(Calendar.DAY_OF_WEEK);
switch (mydate) {
case 1:
showDate = "星期日";
break;
case 2:
showDate = "星期一";
break;
case 3:
showDate = "星期二";
break;
case 4:
showDate = "星期三";
break;
case 5:
showDate = "星期四";
break;
case 6:
showDate = "星期五";
break;
default:
showDate = "星期六";
break;
}
return showDate;
}
/**
* @根据日期时间,获取当前时间是星期几,“星期日”
* @return String
*/
public static String getDayInWeek(Date date) {
if (date == null)
return "";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E");
return simpleDateFormat.format(date);
}
/**
* @获取当前时间在这个月的天
* @return String
*/
public static String getDayInMonth() {
Date today = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d");
return simpleDateFormat.format(today);
}
/**
* @根据日期,获取当前时间在这个月的天
* @return String
*/
public static String getDayInMonth(Date date) {
if (date == null)
return "";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d");
return simpleDateFormat.format(date);
}
/**
* @根据在本年当中,获取月份格式"M"
* @return String
*/
public static String getMonthInYear() {
Date today = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("M");
return simpleDateFormat.format(today);
}
/**
* @根据在本年当中,获取月份格式"MM"
* @return String
*/
public static String getMonthInYear2() {
java.util.Date today = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
return simpleDateFormat.format(today);
}
/**
* @根据日期,获取月份格式"MM"
* @return String
*/
public static String getMonthInYear2(String formatDate) {
java.util.Date day = getDate(formatDate, "yyyy-MM-dd");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
return simpleDateFormat.format(day);
}
/**
* @获取当前时间的年,格式"yyyy"
* @return String
*/
public static String getYear() {
java.util.Date today = new java.util.Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
return simpleDateFormat.format(today);
}
/**
* @根据时间,获取年,格式"yyyy"
* @return String
*/
public static String getYear(String formatDate) {
java.util.Date day = getDate(formatDate, "yyyy-MM-dd");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
return simpleDateFormat.format(day);
}
/**
* @根据日期以及日期格式,获取日期字符串表达
* @return String
*/
public static String getFormatDate(java.util.Date thisDate, String format) {
if (thisDate == null)
return "";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.format(thisDate);
}
/**
* @根据日期以及日期格式,获取日期
* @return String
*/
public static java.util.Date getDate(String date, String format) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
try {
return simpleDateFormat.parse(date);
} catch (ParseException ex) {
return null;
}
}
/**
* @返回系统当前月的第一天所表示的日期
* @param monthDate
* Date
* @return Date
*/
public static java.util.Date getFirstDateMonth() {
try {
String date_s = getYear() + "-" + getMonthInYear2() + "-01";
return getDate(date_s, "yyyy-MM-dd");
} catch (Exception er) {
er.printStackTrace();
}
return null;
}
/**
* @返回系统当前月的第一天所表示的日期
* @param monthDate
* Date
* @return Date
*/
public static java.util.Date getFirstDateMonth(String formatDate) {
try {
String date_s = getYear(formatDate) + "-"
+ getMonthInYear2(formatDate) + "-01";
return getDate(date_s, "yyyy-MM-dd");
} catch (Exception er) {
er.printStackTrace();
}
return null;
}
/**
* @返回系统当前月的第一天所表示的日期
* @param monthDate
* Date
* @return Date
*/
public static String getFirstDateMonthToString() {
try {
return getYear() + "-" + getMonthInYear2() + "-01";
} catch (Exception er) {
er.printStackTrace();
}
return null;
}
/**
* @返回系统当前月的第一天所表示的日期
* @param monthDate
* Date
* @return Date
*/
public static String getFirstDateMonthToString(String formatDate) {
try {
return getYear(formatDate) + "-" + getMonthInYear2(formatDate)
+ "-01";
} catch (Exception er) {
er.printStackTrace();
}
return null;
}
/**
* @返回系统当前月的最后一天所表示的日期
* @return Date
*/
public static java.util.Date getLastDateMonth() {
try {
int lastday = getDaySumInTheMonth();
String date_s;
if (lastday < 10)
date_s = getYear() + "-" + getMonthInYear2() + "-0" + lastday;
else
date_s = getYear() + "-" + getMonthInYear2() + "-" + lastday;
return getDate(date_s, "yyyy-MM-d");
} catch (Exception er) {
er.printStackTrace();
}
return null;
}
/**
* @返回系统当前月的最后一天所表示的日期
* @return Date
*/
public static String getLastDateMonthToString(String formatDate) {
try {
int lastday = getDaySumInTheMonth(formatDate);
if (lastday < 10)
return getYear(formatDate) + "-" + getMonthInYear2(formatDate)
+ "-0" + lastday;
else
return getYear(formatDate) + "-" + getMonthInYear2(formatDate)
+ "-" + lastday;
} catch (Exception er) {
er.printStackTrace();
}
return null;
}
/**
* @根据时间,返回最后一天所表示的日期
* @param formatDate
* String
* @return Date
*/
public static java.util.Date getLastDateMonth(String formatDate) {
try {
int lastday = getDaySumInTheMonth(formatDate);
String month = getMonthInYear2(formatDate);
String year = getYear(formatDate);
String date_s;
if (lastday < 10)
date_s = year + "-" + month + "-0" + lastday;
else
date_s = year + "-" + month + "-" + lastday;
System.out.println(date_s);
return getDate(date_s, "yyyy-MM-d");
} catch (Exception er) {
er.printStackTrace();
}
return null;
}
/**
* @获得这个月的天数
* @return int
*/
public static int getDaySumInTheMonth() {
String month = DayFormat.getMonthInYear();
if (month.equalsIgnoreCase("12"))
return 31;
int mon = Integer.parseInt(month);
java.util.Date date = DayFormat.getDate(DayFormat.getYear() + "-"
+ (mon + 1) + "-" + "1", "yyyy-MM-d");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, -1);
return Integer.parseInt(new SimpleDateFormat("dd")
.format(cal.getTime()));
}
/**
* @获得所在日期月份的天数
* @return int
*/
public static int getDaySumInTheMonth(java.util.Date date) {
String month = new SimpleDateFormat("MM").format(date);
if (month.equalsIgnoreCase("12"))
return 31;
int mon = Integer.parseInt(month);
java.util.Date date2 = DayFormat.getDate(new SimpleDateFormat("yyyy")
.format(date)
+ "-" + (mon + 1) + "-" + "1", "yyyy-MM-d");
Calendar cal = Calendar.getInstance();
cal.setTime(date2);
cal.add(Calendar.DATE, -1);
return Integer.parseInt(new SimpleDateFormat("dd")
.format(cal.getTime()));
}
/**
* @获得所在日期月份的天数
* @return int
*/
public static int getDaySumInTheMonth(String formatDate) {
String month = DayFormat.getMonthInYear2(formatDate);
if (month.equalsIgnoreCase("12"))
return 31;
int mon = Integer.parseInt(month);
java.util.Date date = DayFormat.getDate(DayFormat.getYear(formatDate)
+ "-" + (mon + 1) + "-" + "1", "yyyy-MM-d");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, -1);
return Integer.parseInt(new SimpleDateFormat("dd")
.format(cal.getTime()));
}
/**
* @把日期格式转换成字符串格式,格式为'yyyy-MM-dd'
* @param date
* Date
* @return String
*/
public static String getFormatDate(java.util.Date date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return simpleDateFormat.format(date);
}
public final static String FormatDay1 = "yyyy-MM-dd";
public final static String FormatDay2 = "yyyy年MM月dd日";
public final static String FormatDay3 = "yyyy年MM月dd日 HH:mm:ss";
public final static String FormatDay4 = "yyyy-MM-dd HH:mm";
public static final String Format24Datetime1 = "yyyy-MM-dd HH:mm:ss";// 24
public static final String Format12Datetime1 = "yyyy-MM-dd hh:mm:ss";// 12
/**
* 获取当前时间
*
* @return
*/
public static String getToday() {
return getToday(FormatDay1);
}
public static java.sql.Timestamp getCurrentTimestamp() {
return new Timestamp(System.currentTimeMillis());
}
public static java.util.Date getCurrentUtilDate() {
return new java.util.Date(System.currentTimeMillis());
}
public static String getToday(String formatDay) {
java.util.Date today = new java.util.Date();
SimpleDateFormat df = new SimpleDateFormat(formatDay);
return df.format(today).toString();
}
public static String get24DateTime() {
return get24DateTime(Format24Datetime1);
}
public static String get24DateTime(String format24DateTime) {
java.util.Date today = new java.util.Date();
SimpleDateFormat df = new SimpleDateFormat(format24DateTime);
return df.format(today).toString();
}
public static String get12DateTime() {
return get12DateTime(Format12Datetime1);
}
public static String get12DateTime(String format12DateTime) {
java.util.Date today = new java.util.Date();
SimpleDateFormat df = new SimpleDateFormat(format12DateTime);
return df.format(today).toString();
}
public static String get24DateTime(Timestamp timestamp) {
SimpleDateFormat df = new SimpleDateFormat(Format24Datetime1);
return df.format(timestamp).toString();
}
public static String get12DateTime(Timestamp timestamp) {
SimpleDateFormat df = new SimpleDateFormat(Format12Datetime1);
return df.format(timestamp).toString();
}
// STRING到日期
public static java.sql.Date stringToDate(String dateStr) {
return java.sql.Date.valueOf(dateStr);
}
public static java.sql.Timestamp stringToTimestamp(String timestampStr) {
if (timestampStr == null || timestampStr.length() < 1)
return null;
return java.sql.Timestamp.valueOf(timestampStr);
}
public static java.sql.Timestamp stringToTimestamp2(String dateStr) {
if (dateStr == null || dateStr.length() < 1)
return null;
return java.sql.Timestamp.valueOf(dateStr + " 00:00:00.000000000");
}
public static java.sql.Timestamp stringToTimestamp3(String dateStr) {
if (dateStr == null || dateStr.length() < 1)
return null;
return java.sql.Timestamp.valueOf(dateStr + ":00.000000000");
}
public static java.sql.Time stringToTime(String timeStr) {
return java.sql.Time.valueOf(timeStr);
}
// 日期到STRING
public static String dateToString(java.sql.Date datee) {
return datee.toString();
}
public static String timestampToString(java.sql.Timestamp timestampe) {
return timestampe.toString();
}
public static String timestampToStringForFormat(Timestamp timestamp,
String format) {
if (timestamp == null)
return "";
SimpleDateFormat df = new SimpleDateFormat(format);
return df.format(timestamp).toString();
}
public static String getTimestampToDateTime15Len(
java.sql.Timestamp timestampe) {
if (timestampe == null || timestampe.toString().length() < 1)
return "";
return timestampe.toString().substring(0, 16);
}
public static String timeToString(java.sql.Time timee) {
return timee.toString();
}
public static java.sql.Timestamp StringToTimestamp(String dateString)
throws ParseException {
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
FormatDay1);
formatter.setLenient(false);
return new java.sql.Timestamp((formatter.parse(dateString).getTime()));
}
public static java.sql.Timestamp StringToTimestampAll(String dateString)
throws ParseException {
return java.sql.Timestamp.valueOf(dateString);
}
//获得周日的日期
public static String getSunday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
//获得周一的日期
public static String getMonday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
//获得周二的日期
public static String getTuesday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.TUESDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
//获得周三的日期
public static String getWednesday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.WEDNESDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
//获得周四的日期
public static String getThursday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.THURSDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
//获得周五的日期
public static String getFriday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
//获得周六的日期
public static String getSaturday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.SATURDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
public static void main(String args[]) {
try {
String d = "2007-05-15 19:23:48.703";
System.out.println(d);
java.sql.Timestamp timestampe = java.sql.Timestamp.valueOf(d);
System.out.println(timestampe.toString());
} catch (Exception er) {
er.printStackTrace();
}
}
发表评论
-
memcache
2011-10-11 09:33 1118memcache 详解:http://sosuny.iteye ... -
加 解密
2011-10-11 09:29 624http://8366.iteye.com/blog/5695 ... -
java 类初始化顺序
2010-03-26 11:23 835在Java中,对于静态变量 ... -
java 基础深入
2009-10-28 09:40 876父类引用指向子类对象 ... -
java 数字格式化
2009-10-13 11:26 1523DecimalFormat 是 NumberFormat 的一 ... -
位移运算
2009-09-10 13:50 1200-14 >>2 =-4 14 : 0000 111 ... -
字符串截取
2009-06-16 10:58 999写一个方法,获得一个字符串的子串,比如第一个参数“AB汉字CD ...
相关推荐
在Java编程中,时间控制是常见的需求,尤其是在开发用户界面或者进行定时任务处理时。Java标准库提供了多种方式来...通过阅读和分析源代码,我们可以深入了解这些实现细节,从而更好地理解和应用这些Java时间控制技术。
Java 时间格式大全 Java 中的时间格式是指使用 Java 语言来处理和操作日期和时间的方式。Java 提供了多种方式来处理时间格式,包括使用 `java.util.Date`、`java.util.Calendar`、`java.text.SimpleDateFormat` 等...
java时间工具类按照年月周维度在时间区间内输出时间点
Java时间大小的比较 date java
java 时间片算法 发生的泥泞i萨芬第几集
这篇博客“转 Java校正电脑时间(java 时间同步)”主要探讨了如何在Java中实现这一功能。 首先,Java中的日期和时间API包括`java.util.Date`、`java.util.Calendar`,以及从Java 8开始引入的更现代的`java.time`包。...
java 时间 datatime 工具类
标题"java时间控件"指的是在Java应用程序中实现或使用这样的控件。描述中提到的“判断时间格式”意味着可能涉及对用户输入的时间字符串进行有效性验证,确保它符合预设的日期和时间格式。 在Java中,日期和时间处理...
Java时间格式转换是Java开发中常见的任务,涉及到日期和时间的处理。在Java中,主要通过`java.util.Date`、`java.util.Calendar`以及`java.text.SimpleDateFormat`等类来完成。下面将详细介绍这些类和方法在时间格式...
java时间管理系统(springboot+mysql+vue完整源码+说明文档+LW) 时间管理系统的功能分为管理员,用户两个部门,系统的主要功能包括首页,个人中心,系统公告管理,用户管理,时间分类管理,事件数据管理,目标数据...
Java时间与日期处理是编程中的常见任务,尤其是在开发业务应用或者数据分析时。Java提供了多种类库来处理日期和时间,包括早期的`java.util.Date`和`Calendar`,以及Java 8引入的`java.time`包。这个"JAVA时间和日期...
3. **系统时间和Java时间**: - Java获取系统时间通常是通过调用本地操作系统接口,因此,系统时间的准确性直接影响到Java程序。 - 如果系统时间不正确或与Java时区设置冲突,可能导致获取时间出错。 4. **...
Java时间工具是一个实用的软件应用,它集合了多种与时间管理相关的功能,如带指针的圆形盘表、闹钟、定时关机以及模拟屏保。这个应用是用Java编程语言开发的,Java以其跨平台的特性使得该工具能够在各种操作系统上...
JAVA时间工具类(计算法定工作日工时):计算某个日期加上几个工作日后的一个工作日期(除周末和法定节假日),计算某个日期后一天的工作日期(除周末和法定节假日),计算两个日期的真实工作日(除周末和法定节假日),...
在Java编程语言中,时间处理是一项至关重要的任务,涉及到日期、时间和时区的管理。Java提供了多种类库来处理时间,如`java.util.Date`, `java.util.Calendar`, `java.text.SimpleDateFormat`, `java.time`包(自...
基于java的开发源码-xk-time Java时间工具包.zip 基于java的开发源码-xk-time Java时间工具包.zip 基于java的开发源码-xk-time Java时间工具包.zip 基于java的开发源码-xk-time Java时间工具包.zip 基于java的开发...
Java时间日期处理是编程中常见的任务,特别是在处理与时间相关的逻辑和数据存储时。本文将深入探讨Java中处理时间日期的几个核心类及其用途。 首先,`java.util.Date`是Java标准库中处理时间日期的基本类,它表示自...
在上述提供的代码段中,我们可以看到几个关于Java时间处理的关键知识点。这些方法主要用于获取当前时间,格式化时间字符串,以及将字符串转换为日期对象。 1. **日期对象创建与获取**:`java.util.Date` 类是Java中...
java 时间工具类 java 时间工具类java 时间工具类java 时间工具类java 时间工具类 java 时间工具类java 时间工具类java 时间工具类java 时间工具类 java 时间工具类java 时间工具类java 时间工具类java 时间工具类 ...
Java时间转换 Java时间转换是指在Java编程语言中将日期和时间从一种格式转换为另一种格式的过程。这种转换对于在不同的应用程序中使用日期和时间非常重要。 在Java中,日期和时间可以使用java.util.Date类和java....