【转载】android 工具类 DateUtil,可以用它方便的进行日期的操作

本文转载自:http://blog.csdn.net/xuduzhoud/article/details/27526177

全部代码如下:

DateUtil.java

package com.example.tset;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期操作工具类.
 *
 * @author shimiso
 * http://blog.csdn.net/xuduzhoud/article/details/27526177
 */

public class DateUtil {

    private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static Date str2Date(String str) {
        return str2Date(str, null);
    }

    public static Date str2Date(String str, String format) {
        if (str == null || str.length() == 0) {
            return null;
        }
        if (format == null || format.length() == 0) {
            format = FORMAT;
        }
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            date = sdf.parse(str);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;

    }

    public static Calendar str2Calendar(String str) {
        return str2Calendar(str, null);

    }

    public static Calendar str2Calendar(String str, String format) {

        Date date = str2Date(str, format);
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        return c;

    }

    public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss
        return date2Str(c, null);
    }

    public static String date2Str(Calendar c, String format) {
        if (c == null) {
            return null;
        }
        return date2Str(c.getTime(), format);
    }

    public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss
        return date2Str(d, null);
    }

    public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss
        if (d == null) {
            return null;
        }
        if (format == null || format.length() == 0) {
            format = FORMAT;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String s = sdf.format(d);
        return s;
    }

    public static String getCurDateStr() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-"
                + c.get(Calendar.DAY_OF_MONTH) + "-"
                + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE)
                + ":" + c.get(Calendar.SECOND);
    }

    /**
     * 获得当前日期的字符串格式
     *
     * @param format
     * @return
     */
    public static String getCurDateStr(String format) {
        Calendar c = Calendar.getInstance();
        return date2Str(c, format);
    }

    // 格式到秒
    public static String getMillon(long time) {

        return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time);

    }

    // 格式到天
    public static String getDay(long time) {

        return new SimpleDateFormat("yyyy-MM-dd").format(time);

    }

    // 格式到毫秒
    public static String getSMillon(long time) {

        return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time);

    }

    /*
     输入的是String,格式诸如20120102,实现加一天的功能,返回的格式为String,诸如20120103
     */
    public static String stringDatePlus(String row) throws ParseException{
        String year=row.substring(0, 4);
        String month=row.substring(4,6);
        String day=row.substring(6);
        String date1=year+"-"+month+"-"+day;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date startDate=sdf.parse(date1);
        Calendar cd = Calendar.getInstance();
        cd.setTime(startDate);
        cd.add(Calendar.DATE, 1);
        String dateStr =sdf.format(cd.getTime());
        String year1=dateStr.substring(0,4);
        String month1=dateStr.substring(5,7);
        String day1=dateStr.substring(8);
        return year1+month1+day1;
    }

    /*
     输入的是String,格式诸如20120102,实现减一天的功能,返回的格式为String,诸如20120101
     */
    public static String stringDateDecrease(String row) throws ParseException{
        String year=row.substring(0, 4);
        String month=row.substring(4,6);
        String day=row.substring(6);
        String date1=year+"-"+month+"-"+day;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date startDate=sdf.parse(date1);
        Calendar cd = Calendar.getInstance();
        cd.setTime(startDate);
        cd.add(Calendar.DATE, -1);
        String dateStr =sdf.format(cd.getTime());
        String year1=dateStr.substring(0,4);
        String month1=dateStr.substring(5,7);
        String day1=dateStr.substring(8);
        return year1+month1+day1;
    }

    /*
     输入的格式为String,诸如20120101,返回的格式为String,诸如2012-01-01
     */
    public static String stringDateChange(String date)
    {
        if(date.length()=="20120101".length()){
            String year=date.substring(0, 4);
            String month=date.substring(4,6);
            String day=date.substring(6);
            return year+"-"+month+"-"+day;
        }else{
            return date;
        }

    }
    /**
     * 日期向后推一天
     * @param date 格式:20120101
     * @return  20120102
     */
    public static String tonextday(String date){
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(4,6));
        int day = Integer.parseInt(date.substring(6));
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month-1, day+1);
        Date newdate = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String da = format.format(newdate);
        return da;
    }

    /**
     * 获取当前日期上一周的开始日期 (周日)
     */
    public static String previousWeekByDate(String date) {
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(4,6));
        int day = Integer.parseInt(date.substring(6));
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month-1, day);
        Date newdate = calendar.getTime();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(newdate);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        if(1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
        int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-s);//根据日历的规则,给当前日期减往星期几与一个星期第一天的差值
        cal.add(Calendar.WEEK_OF_YEAR, -1);
        String imptimeBegin = sdf.format(cal.getTime());
//      System.out.println("所在周星期日的日期:"+imptimeBegin);
        return imptimeBegin;
    }

    /**
     * 获取当前日期上一周的结束日期 (周六)
     */
    public static String previousWeekEndDayByDate(String date) {
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(4,6));
        int day = Integer.parseInt(date.substring(6));
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month-1, day);
        Date newdate = calendar.getTime();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(newdate);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        if(1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
        int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek()+(6-s));
        cal.add(Calendar.WEEK_OF_YEAR, -1);
        String imptimeBegin = sdf.format(cal.getTime());
//      System.out.println("星期六的日期:"+imptimeBegin);
        return imptimeBegin;
    }

    /**
     * 获取当前日期当前一周的开始日期 (周日)
     */
    public static String getCurrentWeekFirstDayByDate(String date) {
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(4,6));
        int day = Integer.parseInt(date.substring(6));
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month-1, day);
        Date newdate = calendar.getTime();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(newdate);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        if(1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
        int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-s);//根据日历的规则,给当前日期减往星期几与一个星期第一天的差值 

        String imptimeBegin = sdf.format(cal.getTime());
      //  System.out.println("所在周星期日的日期:"+imptimeBegin);
        return imptimeBegin;
    }
    /**
     * 获取当前日期当前一周的结束日期 (周六)
     */
    public static String getCurrentWeekEndDayByDate(String date) {
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(4,6));
        int day = Integer.parseInt(date.substring(6));
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month-1, day);
        Date newdate = calendar.getTime();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(newdate);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        if(1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
        int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek()+(6-s));

        String imptimeBegin = sdf.format(cal.getTime());
        return imptimeBegin;
    }

/**
 * 返回上一个月的第一天
 * @param date20120304
 * @return  20120201
 */
    public static String previousMonthByDate(String date) {
        // TODO Auto-generated method stub
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(4,6));
        int day = Integer.parseInt(date.substring(6));
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month-2, 1);
        Date newdate = calendar.getTime();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(newdate);
        String imptimeBegin = sdf.format(cal.getTime());
//      System.out.println(imptimeBegin);
        return imptimeBegin;
    }

    /**
     * 返回下一个月的第一天
     * @param date20120304
     * @return  20120401
     */
    public static String nextMonthByDate(String date) {
        // TODO Auto-generated method stub
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(4,6));
        int day = Integer.parseInt(date.substring(6));
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);
        Date newdate = calendar.getTime();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(newdate);
        String imptimeBegin = sdf.format(cal.getTime());
//      System.out.println(imptimeBegin);
        return imptimeBegin;
    }
/**
 * 返回当前月的第一天
 * @param date20120103
 * @return 20120101
 */
    public static String getCurrentMonthFirstDayByDate(String date) {
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(4,6));
        int day = Integer.parseInt(date.substring(6));
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month-1, 1);
        Date newdate = calendar.getTime();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(newdate);
        String imptimeBegin = sdf.format(cal.getTime());
        return imptimeBegin;
    }

}

【转载】android 工具类 DateUtil,可以用它方便的进行日期的操作

时间: 2024-10-13 09:36:49

【转载】android 工具类 DateUtil,可以用它方便的进行日期的操作的相关文章

android 工具类 DateUtil

提取了一些在开发过程中可能会用到的日期相关的函数作为工具类,供大家参考: /** * 日期操作工具类. * * @author shimiso */ public class DateUtil { private static final String FORMAT = "yyyy-MM-dd HH:mm:ss"; public static Date str2Date(String str) { return str2Date(str, null); } public static

android 工具类 数据库管理

数据库工具类,优雅的管理android中的sqlite package csdn.shimiso.eim.db; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; /** * SQLite数据库模板工具类 * *

android 工具类之图片加工

1 /** 2 * 图片加工厂 3 * 4 * @author way 5 * 6 */ 7 public class ImageUtil { 8 /** 9 * 通过路径获取输入流 10 * 11 * @param path 12 * 路径 13 * @return 输入流 14 * @throws Exception 15 * 异常 16 */ 17 public static InputStream getRequest(String path) throws Exception { 18

android 工具类之SharePreference

1 /** 2 * SharedPreferences的一个工具类,调用setParam就能保存String, Integer, Boolean, Float, Long类型的参数 3 * 同样调用getParam就能获取到保存在手机里面的数据 4 * @author xiaanming 5 * 6 */ 7 public class SharedPreferencesUtils { 8 /** 9 * 保存在手机里面的文件名 10 */ 11 private static final Stri

java date 工具类 DateUtil 格式转换

package systems.baseutil.tool; /** * <p>Title: 时间和日期的工具类</p> * <p>Description: DateUtil类包含了标准的时间和日期格式,以及这些格式在字符串及日期之间转换的方法</p> * <p>Copyright: Copyright (c) 2007 advance,Inc. All Rights Reserved</p> * <p>Company:

【Android 工具类】常用工具类(方法)大全

收集常用的工具类或者方法: 1.获取手机分辨率 /** * 获取手机分辨率 */ public static String getDisplayMetrix(Context context) { if (Constant.Screen.SCREEN_WIDTH == 0 || Constant.Screen.SCREEN_HEIGHT == 0) { if (context != null) { int width = 0; int height = 0; SharedPreferences D

【Android 工具类】经常使用工具类(方法)大全

收集经常使用的工具类或者方法: 1.获取手机分辨率 /** * 获取手机分辨率 */ public static String getDisplayMetrix(Context context) { if (Constant.Screen.SCREEN_WIDTH == 0 || Constant.Screen.SCREEN_HEIGHT == 0) { if (context != null) { int width = 0; int height = 0; SharedPreferences

摘录android工具类

1 import android.content.Context; 2 import android.content.pm.PackageInfo; 3 import android.content.pm.PackageManager; 4 import android.content.pm.PackageManager.NameNotFoundException; 5 6 //跟App相关的辅助类 7 public class AppUtils 8 { 9 10 private AppUtil

[Android Pro] 常用的android工具类和库

reference to  : http://blog.csdn.net/lovexieyuan520/article/details/50614086 这篇博客主要记录我认为比较有用的Android开源框架或者工具,我会持续添加,推荐给大家 1.这里面包含了很多的工具类,包括数据校验工具类,文件操作工具类,汉字转拼音工具类等. https://github.com/jingle1267/android-utils 2.CWAC,这里面包含不少android开发中具有普遍用途的库 https:/