DateUtil

将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss

public static Date strToDateLong(String strDate) {

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

ParsePosition pos = new ParsePosition(0);

Date strtodate = formatter.parse(strDate, pos);

return strtodate;

}

获取昨天时间

public static String getYesterday(){

Calendar cal = Calendar.getInstance();

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

String yesterday = new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime());

return yesterday;

}

获取两个日期之间的所有日期(yyyy-MM-dd)

public static List<String> getBetweenDates(Date begin, Date end) {

List<String> result = new ArrayList<String>();

Calendar tempStart = Calendar.getInstance();

tempStart.setTime(begin);

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

while (begin.getTime() <= end.getTime()) {

result.add(df.format(tempStart.getTime()));

tempStart.add(Calendar.DAY_OF_YEAR, 1);

begin = tempStart.getTime();

}

return result;

}

获取两个日期之间的所有日期(yyyy-MM-dd)

public static Map<String,EnvInfoRecordLowHeight> getBetweenDatesForEnvBean(Date begin, Date end) {

Map<String,EnvInfoRecordLowHeight> result = new HashMap<String,EnvInfoRecordLowHeight>();

Calendar tempStart = Calendar.getInstance();

tempStart.setTime(begin);

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

while (begin.getTime() <= end.getTime()) {

result.put(df.format(tempStart.getTime()),new EnvInfoRecordLowHeight());

tempStart.add(Calendar.DAY_OF_YEAR, 1);

begin = tempStart.getTime();

}

return result;

}

转换日期格式

  • 支持:yyyy-MM-dd HH:mm:ss yyyy/MM/dd HH:mm:ss yyyyMMdd HH:mm:ss

  • 支持:yyyy-MM-dd yyyy/MM/dd yyyyMMdd

  • @param strDate

  • @return

public static Date toDate(String strDate){

Date date = null;

try{

if(StrUtil.isNotEmpty(strDate)){

if(strDate.contains(":")){//带时分秒

if(strDate.contains("-")){

date = DateUtil.strToLongDate(strDate);

}else if(strDate.contains("/")){

date = DateUtil.strToLongDateXG(strDate);

}else{

date = DateUtil.strToDateymdHHmmss(strDate);

}

}else{//不带时分秒

if(strDate.contains("-")){

date = DateUtil.strToDate(strDate);

}else if(strDate.contains("/")){

date = DateUtil.strToDateXG(strDate);

}else{

date = DateUtil.strToDateymd(strDate);

}

}

}

}catch(Exception e){

}

return date;

}

将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss

public static String dateToStrLong(Date dateDate) {

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String dateString = formatter.format(dateDate);

return dateString;

}

将长时间格式时间转换为字符串 yyyyMMddHHmmss

public static String dateToStrLong2(Date dateDate) {

SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");

String dateString = formatter.format(dateDate);

return dateString;

}

将短时间格式时间转换为字符串 yyyy-MM-dd

public static String dateToStr(Date dateDate) {

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

String dateString = formatter.format(dateDate);

return dateString;

}

将短时间格式字符串转换为时间 yyyy-MM-dd

public static Date strToDate(String strDate) {

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

ParsePosition pos = new ParsePosition(0);

Date strtodate = formatter.parse(strDate, pos);

return strtodate;

}

将短时间格式字符串转换为时间 yyyy/MM/dd

public static Date strToDateXG(String strDate) {

SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");

ParsePosition pos = new ParsePosition(0);

Date strtodate = formatter.parse(strDate, pos);

return strtodate;

}

将短时间格式字符串转换为时间 yyyyMMdd

public static Date strToDateymd(String strDate) {

SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

ParsePosition pos = new ParsePosition(0);

Date strtodate = formatter.parse(strDate, pos);

return strtodate;

}

将短时间格式字符串转换为时间 yyyyMMdd HH:mm:ss

public static Date strToDateymdHHmmss(String strDate) {

SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HH:mm:ss");

ParsePosition pos = new ParsePosition(0);

Date strtodate = formatter.parse(strDate, pos);

return strtodate;

}

将短时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss

public static Date strToLongDate(String strDate) {

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

ParsePosition pos = new ParsePosition(0);

Date strtodate = formatter.parse(strDate, pos);

return strtodate;

}

将短时间格式字符串转换为时间yyyy/MM/dd HH:mm:ss

public static Date strToLongDateXG(String strDate) {

SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

ParsePosition pos = new ParsePosition(0);

Date strtodate = formatter.parse(strDate, pos);

return strtodate;

}

得到当前年份

public static String getYear(){

        Calendar date = Calendar.getInstance();

        String year = String.valueOf(date.get(Calendar.YEAR));

        return year;

}

得到指定时间的上个月的年

public static int getLastMonthYear(Date date){

Calendar calendar = Calendar.getInstance();//日历对象 

        calendar.setTime(date);

        calendar.add(Calendar.MONTH, -1);//月份减一 

        int month = calendar.get(Calendar.MONTH)+1;

        return month;

}

得到指定时间的上个月

public static int getLastMonth(Date date){

Calendar calendar = Calendar.getInstance();//日历对象 

        calendar.setTime(date);

        calendar.add(Calendar.MONTH, -1);//月份减一 

        return calendar.get(Calendar.YEAR);

}

获取上月最后一天 yyyy-MM-dd HH:mm:ss

public static String getLastDay(Date date){

  //获取当前时间

  Calendar cal = Calendar.getInstance();

  cal.setTime(date);

  //下面可以设置月份,注:月份设置要减1,所以设置1月就是1-1,设置2月就是2-1,如此类推

  cal.set(Calendar.MONTH, 1-1);

  //调到上个月

  cal.add(Calendar.MONTH, -1);

  //得到一个月最最后一天日期(31/30/29/28)

  int MaxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

  //按你的要求设置时间

  cal.set( cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), MaxDay, 23, 59, 59);

  //按格式输出

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  return sdf.format(cal.getTime());

}

获取上月第一天 yyyy-MM-dd HH:mm:ss

public static String getFirstDay(Date date){

获取前一个月第一天

        Calendar calendar1 = Calendar.getInstance();

        calendar1.setTime(date);

        calendar1.add(Calendar.MONTH, -1);

        calendar1.set(Calendar.DAY_OF_MONTH,1);

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

        String firstDay = sdf.format(calendar1.getTime())+" 00:00:00";

        return firstDay;

}

得到现在小时

public static String getHour() {

Date currentTime = new Date();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String dateString = formatter.format(currentTime);

String hour;

hour = dateString.substring(11, 13);

return hour;

}

得到现在分钟

public static String getTime() {

Date currentTime = new Date();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String dateString = formatter.format(currentTime);

String min;

min = dateString.substring(14, 16);

return min;

}

获得指定日期的前一天

public static String getDayBefore(String specifiedDay) {

        Calendar c = Calendar.getInstance(); 

        Date date = null; 

        try { 

            date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay); 

        } catch (ParseException e) { 

            e.printStackTrace(); 

        } 

        c.setTime(date); 

        int day = c.get(Calendar.DATE); 

        c.set(Calendar.DATE, day - 1); 



        String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()); 

        return dayBefore; 

    } 

获得指定日期的后一天

public static String getDayAfter(String specifiedDay) {

        Calendar c = Calendar.getInstance(); 

        Date date = null; 

        try { 

            date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay); 

        } catch (ParseException e) { 

            e.printStackTrace(); 

        } 

        c.setTime(date); 

        int day = c.get(Calendar.DATE); 

        c.set(Calendar.DATE, day + 1); 



        String dayAfter = new SimpleDateFormat("yyyy-MM-dd") 

                .format(c.getTime()); 

        return dayAfter; 

    }

计算两个日期相差的天数

public static int getDifDay(Date date1,Date date2) throws ParseException{

    if(null == date1){

    return 0;

    }

    if(null == date2){

    return 0;

    }

    long from = date2.getTime();

    long to = date1.getTime();

    int days = (int) ((to - from)/(1000 * 60 * 60 * 24));

    return days;

    }

计算两个日期相差的天数

public static int getDifDay(String date1,String date2) throws ParseException{

    SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//如2016-08-10 20:40 

    Date fromDate = simpleFormat.parse(date2); 

    Date toDate = simpleFormat.parse(date1); 

    long from = fromDate.getTime();

    long to = toDate.getTime();

    int days = (int) ((to - from)/(1000 * 60 * 60 * 24));

    return days;

    }

获得指定日期的前N天

public static String getDayBefore(String specifiedDay,int days) {

        Calendar c = Calendar.getInstance(); 

        Date date = null; 

        try { 

            date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay); 

        } catch (ParseException e) { 

            e.printStackTrace(); 

        } 

        c.setTime(date); 

        int day = c.get(Calendar.DATE); 

        c.set(Calendar.DATE, day - days); 



        String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c 

                .getTime()); 

        return dayBefore; 

    }

获得指定日期的后N天

public static String getDayAfter(String specifiedDay, int days) {

        Calendar c = Calendar.getInstance(); 

        Date date = null; 

        try { 

            date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay); 

        } catch (ParseException e) { 

            e.printStackTrace(); 

        } 

        c.setTime(date); 

        int day = c.get(Calendar.DATE); 

        c.set(Calendar.DATE, day + days); 



        String dayAfter = new SimpleDateFormat("yyyy-MM-dd") 

                .format(c.getTime()); 

        return dayAfter; 

    }

几分钟前的时间

public static Date getBeforeForDate(int minute){

    Date now = new Date();

    Date now_10 = new Date(now.getTime() - minute*60*1000); //10分钟前的时间

    return now_10;

    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容

  • ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year:...
    雨一流阅读 674评论 0 2
  • longaaaa =14200666; Console.WriteLine(aaaa.ToString("N0")...
    鱼落于天阅读 915评论 0 1
  • 看电影时,心里想着别的事,有些分心,估计也错过了不少精彩之处。转念一想,都已经这样了,不如写下我的感受,待到他日再...
    阿诺蓝阅读 398评论 0 2
  • 别做纸老虎,纸老虎会招来真狼。真狼,你是打不过的。
    素心说阅读 130评论 0 0
  • 一、净利润 概念理解 1、毛利润和净利润不成正比,毛利率高未必净利率高,因为在毛利润高,但是“销售费用”占“营业收...
    Joanna_乔阅读 194评论 0 1