java获取时间的方法是_JAVA获取时间的N种方法

import java.sql.Timestamp;

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

import java.util.Locale;

public class DateUtils {

/**

* 获得当前时间,格式yyyy-MM-dd hh:mm:ss

* @param format

* @return

*/

public static String getCurrentDate(){

return getCurrentDate("yyyy-MM-dd HH:mm:ss");

}

/**

* 获得当前时间,格式自定义

* @param format

* @return

*/

public static String getCurrentDate(String format){

Calendar day=Calendar.getInstance();

day.add(Calendar.DATE,0);

SimpleDateFormat sdf=new SimpleDateFormat(format);//"yyyy-MM-dd"

String date = sdf.format(day.getTime());

return date;

}

/**

* 获得昨天时间,格式自定义

* @param format

* @return

*/

public static String getYesterdayDate(String format){

Calendar day=Calendar.getInstance();

day.add(Calendar.DATE,-1);

SimpleDateFormat sdf=new SimpleDateFormat(format);//"yyyy-MM-dd"

String date = sdf.format(day.getTime());

return date;

}

/**

* @param date1 需要比较的时间 不能为空(null),需要正确的日期格式 ,如:2009-09-12

* @param date2 被比较的时间 为空(null)则为当前时间

* @param stype 返回值类型 0为多少天,1为多少个月,2为多少年

* @return

* 举例:

* compareDate("2009-09-12", null, 0);//比较天

* compareDate("2009-09-12", null, 1);//比较月

* compareDate("2009-09-12", null, 2);//比较年

*/

public static int compareDate(String startDay,String endDay,int stype){

int n = 0;

//String[] u = {"天","月","年"};

String formatStyle = stype==1?"yyyy-MM":"yyyy-MM-dd";

endDay = endDay==null?getCurrentDate("yyyy-MM-dd"):endDay;

DateFormat df = new SimpleDateFormat(formatStyle);

Calendar c1 = Calendar.getInstance();

Calendar c2 = Calendar.getInstance();

try {

c1.setTime(df.parse(startDay));

c2.setTime(df.parse(endDay));

} catch (Exception e3) {

System.out.println("wrong occured");

}

//List list = new ArrayList();

while (!c1.after(c2)) { // 循环对比,直到相等,n 就是所要的结果

//list.add(df.format(c1.getTime())); // 这里可以把间隔的日期存到数组中 打印出来

n++;

if(stype==1){

c1.add(Calendar.MONTH, 1); // 比较月份,月份+1

}

else{

c1.add(Calendar.DATE, 1); // 比较天数,日期+1

}

}

n = n-1;

if(stype==2){

n = (int)n/365;

}

// System.out.println(startDay+" -- "+endDay+" 相差多少"+u[stype]+":"+n);

return n;

}

/**

* 判断时间是否符合时间格式

*/

public static boolean isDate(String date, String dateFormat) {

if (date != null) {

java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(dateFormat);

format.setLenient(false);

try {

format.format(format.parse(date));

} catch (ParseException e) {

// TODO Auto-generated catch block

return false;

}

return true;

}

return false;

}

/**

* 实现给定某日期,判断是星期几

* date:必须yyyy-MM-dd格式

*/

public static String getWeekday(String date){

SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");

SimpleDateFormat sdw = new SimpleDateFormat("E");

Date d = null;

try {

d = sd.parse(date);

} catch (ParseException e) {

e.printStackTrace();

}

return sdw.format(d);

}

/**

* 用来全局控制 上一周,本周,下一周的周数变化

*/

private static int weeks = 0;

/**

* 获得当前日期与本周一相差的天数

*/

private static int getMondayPlus() {

Calendar cd = Calendar.getInstance();

// 获得今天是一周的第几天,星期日是第一天,星期二是第二天......

int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);

if (dayOfWeek == 1) {

return -6;

} else {

return 2 - dayOfWeek;

}

}

/**

* 获得本周星期一的日期

*/

public static String getCurrentMonday(String format) {

weeks = 0;

int mondayPlus = getMondayPlus();

Calendar currentDate=Calendar.getInstance();

currentDate.add(Calendar.DATE, mondayPlus);

SimpleDateFormat sdf=new SimpleDateFormat(format);//"yyyy-MM-dd"

String date = sdf.format(currentDate.getTime());

return date;

}

/**

* 获得上周星期一的日期

*/

public static String getPreviousMonday(String format) {

weeks--;

int mondayPlus = getMondayPlus();

Calendar currentDate=Calendar.getInstance();

currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);

SimpleDateFormat sdf=new SimpleDateFormat(format);//"yyyy-MM-dd"

String date = sdf.format(currentDate.getTime());

return date;

}

/**

* 获得下周星期一的日期

*/

public static String getNextMonday(String format) {

weeks++;

int mondayPlus = getMondayPlus();

// GregorianCalendar currentDate = new GregorianCalendar();

Calendar currentDate=Calendar.getInstance();

currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);

SimpleDateFormat sdf=new SimpleDateFormat(format);//"yyyy-MM-dd"

String date = sdf.format(currentDate.getTime());

return date;

}

/**

* 获得相应周的周日的日期

* 此方法必须写在getCurrentMonday,getPreviousMonday或getNextMonday方法之后

*/

public static String getSunday(String format) {

int mondayPlus = getMondayPlus();

Calendar currentDate=Calendar.getInstance();

currentDate.add(Calendar.DATE, mondayPlus + 7 * weeks + 6);

SimpleDateFormat sdf=new SimpleDateFormat(format);//"yyyy-MM-dd"

String date = sdf.format(currentDate.getTime());

return date;

}

/**

*method 将字符串类型的日期转换为一个timestamp(时间戳记java.sql.Timestamp)

*@param dateString 需要转换为timestamp的字符串

*@return dataTime timestamp

*/

public static Timestamp string2Time(String dateString) {

DateFormat dateFormat;

dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);// 设定格式

dateFormat.setLenient(false);

java.util.Date date = null;

try {

date = dateFormat.parse(dateString);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// java.sql.Timestamp dateTime = new java.sql.Timestamp(date.getTime());

return new Timestamp(date.getTime());// Timestamp类型,timeDate.getTime()返回一个long型

}

/**

*method 将字符串类型的日期转换为一个Date(java.sql.Date)

*

* @param dateString

* 需要转换为Date的字符串

*@return dataTime Date

*/

public final static java.sql.Date string2Date(String dateString) {

DateFormat dateFormat;

dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

dateFormat.setLenient(false);

java.util.Date date = null;

try {

date = dateFormat.parse(dateString);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// java.sql.Date dateTime = new java.sql.Date(date.getTime());// sql类型

return new java.sql.Date(date.getTime());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值