⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit.java

📁 一个直销用的管理软件
💻 JAVA
字号:
package com.susssoft.richjl.common;

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

import org.apache.log4j.Logger;

/**
 * 此类中包含一些通用方法,将来可以用于移植
 * 
 * @author 付祖远
 */
public class Unit {
    private static final Logger log = Logger.getLogger(Unit.class);

    /**
     * 得到一个类似序列的编号,比如经销商编号000080,那么后面的将会以000081继续延伸
     * 
     * @return String 得到编号
     */
    public static String getSequenceID(String initStr) {
        int strLength = initStr.length();
        int strValue = Integer.parseInt(initStr) + 1;
        String returnValue = String.valueOf(strValue);
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < strLength - returnValue.length(); i++) {
            buf.append("0");
        }
        return buf.toString() + returnValue;
    }

    /**
     * 将字符串进行转码
     * 
     * @param str
     *            将要转化的字符串
     */
    public static String transEncoding(String str) {
        String newstr = "";
        try {
            newstr = new String(str.getBytes("ISO-8859-1"), "GBK");
        } catch (java.io.UnsupportedEncodingException e) {
            log.error(e);
        }
        return newstr;
    }

    /**
     * 得到转化后的时间:2005-02-02 00:00:00.0变为2005年2月2日
     * 
     * @param oldDate
     *            将要格式化的日期
     */
    public static final String getFormatDate(String oldDate) {
        if(oldDate.indexOf(" ")!=-1){
            oldDate = oldDate.substring(0, oldDate.indexOf(" "));
        }
        
        String[] str = oldDate.split("-");
        StringBuffer buf = new StringBuffer();
        buf.append(str[0]).append("年").append(str[1]).append("月")
                .append(str[2]).append("日");
        return buf.toString();
    }

    /**
     * 得到转化后的时间:2005-02-02 00:00:00.0变为2005年02月
     * 
     * @param oldDate
     *            将要格式化的日期
     */
    public static final String getFormatDateNoDay(String oldDate) {
        oldDate = oldDate.substring(0, oldDate.indexOf(" "));
        String[] str = oldDate.split("-");
        StringBuffer buf = new StringBuffer();
        buf.append(str[0]).append("年").append(str[1]).append("月");
        return buf.toString();
    }
    
    /**
     * 得到转化后的时间:2005-02-02 00:00:00.0变为2005-02
     * 
     * @param oldDate
     *            将要格式化的日期
     */
    public static final String getFormatDateNoDayByEn(String oldDate) {
        oldDate = oldDate.substring(0, oldDate.indexOf(" "));
        String[] str = oldDate.split("-");
        StringBuffer buf = new StringBuffer();
        buf.append(str[0]).append("-").append(str[1]);
        return buf.toString();
    }
    
    /**
     * 得到转化后的时间:2005-02-02 00:00:00 变为2005-02-02
     * 
     * @param oldDate
     *            将要格式化的日期
     * @return 返回日期
     */
    public static final String getTrimDateNoTimer(String oldDate) {
        String[] str = oldDate.split(" ");
        return str[0];
    }

    /**
     * 得到系统当前时间:以2000-12-10的形式来表示
     * 
     * @author fuzuyuan
     * @return String 返回当前日期
     */
    public static final String getCurrentTimer() {
        Calendar ca = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(ca.getTime());
    }
    
    /*
     * 得到系统当前时间:并以数组的形式返回,分别存储年,月,日
     */
    public static final String[] getCurrentTimerSplit(){
    	Calendar ca = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentTimer = sdf.format(ca.getTime());
        return currentTimer.split("-");
    }
    
    /**
     * 得到系统当前时间:以2000-12的形式来表示
     * 
     * @author fuzuyuan
     * @return String 返回当前日期
     */
    public static final String getCurrentTimerNoDay() {
        Calendar ca = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(ca.getTime());
    }

    /**
     * 得到系统当前时间:以2000年12月的形式来表示
     * 
     * @author fuzuyuan
     * @return String 返回当前日期
     */
    public static final String getCurrentTimerNoDayChina() {
        Calendar ca = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
        return sdf.format(ca.getTime());
    }
    
    /**
     * 将精确到分的钱以实数来表示
     * 
     * @param money
     */
    public static final String getMoneyByY(int money) {
        String returnValue = "";
        returnValue = String.valueOf((float) money / 100);
        return returnValue;
    }

    /**
     * 将精确到分的钱以实数来表示
     * 
     * @param money
     */
    public static final String getMoneyByY(float money) {
        String returnValue = "";
        returnValue = String.valueOf(money / 100);
        return returnValue;
    }
    
    /**
     * 查询指定的日期是属于一年中的第几周
     * 
     * @param queryDate
     *            要查询的日期
     * @return 返回这天是一年中的第几周
     */
    public static final int getWeekOfYear(String queryDate) {
        int returnValue = 0;
        String[] str = queryDate.split("-");
        int[] dateInt = new int[3];
        dateInt[0] = Integer.parseInt(str[0]);
        dateInt[1] = Integer.parseInt(str[1]) - 1;
        dateInt[2] = Integer.parseInt(str[2]);
        Calendar cal1 = Calendar.getInstance();
        cal1.set(dateInt[0], dateInt[1], dateInt[2], 0, 0, 0);
        return cal1.get(java.util.Calendar.WEEK_OF_YEAR);
    }

    /**
     * 查询指定的日期是属于一年中的第几周,属于哪一年,属于哪一个月
     * @param queryDate 要查询的日期
     * @return 返回一个三维数组,依次存入年,月,第几周
     */
    public static final int[] getWeekMonthYearOfYear1(String queryDate) {
        int[] returnValue = new int[3];
        String[] str = queryDate.split("-");
        int[] dateInt = new int[3];
        dateInt[0] = Integer.parseInt(str[0]);
        dateInt[1] = Integer.parseInt(str[1]) - 1;
        dateInt[2] = Integer.parseInt(str[2]) - 1 ; //因为是外国人的记日期方法,它们是以星期天开始记的,以星期天为第一天,所以此处应该减1
        Calendar cal1 = Calendar.getInstance();
        cal1.set(dateInt[0], dateInt[1], dateInt[2], 0, 0, 0);
        
        returnValue[0] = cal1.get(Calendar.YEAR);
        returnValue[1] = cal1.get(Calendar.MONTH) + 1;
        returnValue[2] = cal1.get(Calendar.WEEK_OF_YEAR);
        
        //得到这一年的1月1号是星期几
        Calendar cal2 = Calendar.getInstance();
        cal2.set(dateInt[0], 0, 1, 0, 0, 0);
        int week = cal2.get(Calendar.DAY_OF_WEEK);
        if(week==1){
        	week = 7;
        } else {
        	week = week -1;
        }
        
        if(week!=1){
            //如果不是星期一,表明日期记得是不准确的
            Calendar cal3 = Calendar.getInstance();
            cal3.set(dateInt[0], 0, -(week+1), 0, 0, 0);
            
            if(dateInt[2] > 7 - week){
            	returnValue[2] = returnValue[2] -1;
            } else if(dateInt[1]==11 && returnValue[2]==1){
            	returnValue[0] = cal3.get(Calendar.YEAR);
                returnValue[1] = cal3.get(Calendar.MONTH) + 1;
                returnValue[2] = cal3.get(Calendar.WEEK_OF_YEAR) + 1;
            } else {
                returnValue[0] = cal3.get(Calendar.YEAR);
                returnValue[1] = cal3.get(Calendar.MONTH) + 1;
                if(week == 7){
                	//特别情况,此年的第一天是星期天
                	returnValue[2] = cal3.get(Calendar.WEEK_OF_YEAR);
                } else {
                	returnValue[2] = cal3.get(Calendar.WEEK_OF_YEAR) + 1;
                }
            }
        } else {
        	returnValue[0] = dateInt[0];
            returnValue[1] = dateInt[1] + 1;
            returnValue[2] = cal1.get(Calendar.WEEK_OF_YEAR);
        }
        
        return returnValue;
    }
    
    public static final int[] getWeekMonthYearOfYear(String queryDate){
    	//首先得到年,月,日的整型类型
    	int[] returnValue = new int[3];
        String[] str = queryDate.split("-");
        int[] dateInt = new int[3];
        dateInt[0] = Integer.parseInt(str[0]);
        dateInt[1] = Integer.parseInt(str[1]);
        dateInt[2] = Integer.parseInt(str[2]);
        
        //得到查询那一年的第一天是星期几,以数字来表示,星期一为1,...,星期天为7
        Calendar cal1 = Calendar.getInstance();
        cal1.set(dateInt[0], 0, 1, 0, 0, 0);
        int week = cal1.get(Calendar.DAY_OF_WEEK);
        if(week==1){
        	week = 7;
        } else {
        	week = week -1;
        }
        
        //根据星期来进行逻辑判断,如果week正好是星期一,则为标准记周
        if(week==1){
        	cal1.set(dateInt[0], dateInt[1] - 1, dateInt[2], 0, 0, 0);
        	returnValue[0] = cal1.get(Calendar.YEAR);
            returnValue[1] = cal1.get(Calendar.MONTH) + 1;
            returnValue[2] = cal1.get(Calendar.WEEK_OF_YEAR);
            //得到这一天是星期几
            int tempWeek = cal1.get(Calendar.DAY_OF_WEEK);
            if(tempWeek==1){
            	//说明是星期天
            	returnValue[2] = returnValue[2] -1;
            }
        } else if(week!=1){
        	//如果日期不在第java日期计算的第一周内,则将得到的星期减1即可
        	if(dateInt[2] > (8 - week)){
        		cal1.set(dateInt[0], dateInt[1] - 1, dateInt[2], 0, 0, 0);
            	returnValue[0] = cal1.get(Calendar.YEAR);
                returnValue[1] = cal1.get(Calendar.MONTH) + 1;
                returnValue[2] = cal1.get(Calendar.WEEK_OF_YEAR) - 1;
                //得到这一天是星期几
                int tempWeek = cal1.get(Calendar.DAY_OF_WEEK);
                if(tempWeek==1){
                	//说明是星期天
                	returnValue[2] = returnValue[2] -1;
                }
        	} else if(dateInt[2] <= (8 - week)){
        		//说明这几天是属于前一年的最后一周的
        		cal1.set(dateInt[0], dateInt[1] - 1, -(week+1), 0, 0, 0);
            	returnValue[0] = cal1.get(Calendar.YEAR);
                returnValue[1] = cal1.get(Calendar.MONTH) + 1;
                returnValue[2] = cal1.get(Calendar.WEEK_OF_YEAR) + 1;
                
                //得到此年的前一年的第一天是星期几,以此来判断前年中是否多算了一周,如果第一天不是星期一,则多
                //算了一周
        		cal1.set(dateInt[0]-1, 0, 1, 0, 0, 0);
            	int tempWeek = cal1.get(Calendar.DAY_OF_WEEK);
            	if(tempWeek != 2){
            		//说明第一天不是星期一
            		returnValue[2] = returnValue[2] -1;
            	}
        	}
        }
        
        return returnValue;
    }
    
    /*
     *  
     */
    public static String asHTML(String text) {
        if (text == null) {
            return "";
        }
        StringBuffer results = null;
        char[] orig = null;
        int beg = 0, len = text.length();
        for (int i = 0; i < len; ++i) {
            char c = text.charAt(i);
            switch (c) {
            	case 0:
            	case '&':
            	case '<':
            	case '>':
            	case '\"':
                if (results == null) {
                    orig = text.toCharArray();
                    results = new StringBuffer(len + 10);
                }
                if (i > beg)
                    results.append(orig, beg, i - beg);
                beg = i + 1;
                switch (c) {
                default: //  case 0:
                    continue;
                case '&':
                    results.append("&");
                    break;
                case '<':
                    results.append("<");
                    break;
                case '>':
                    results.append(">");
                    break;
                case '\"':
                    results.append("\"");
                    break;
                }
                break;
            }
        }
        if (results == null)
            return text;
        results.append(orig, beg, len - beg);
        return results.toString();
    }
    
    public static void main(String[] args) {
        int[] value = Unit.getWeekMonthYearOfYear("2005-9-25");
        System.out.println(value[0]);
        System.out.println(value[1]);
        System.out.println(value[2]);
        
        Calendar cal2 = Calendar.getInstance();
        cal2.set(2010, 11, 25, 0, 0, 0);
        System.out.println(cal2.get(Calendar.WEEK_OF_YEAR));
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -