java中常用的方法


格式时间================================================================ ====
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        String defaultDate = bartDateFormat.format(now);
        System.out.println(defaultDate);

获取一天的开始始时间==============================================================
public static Date getBeginDate(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // calendar.set(Calendar.HOUR_OF_DAY, 0);
        // calendar.set(Calendar.MINUTE, 0);
        // calendar.set(Calendar.SECOND, 0);
        // calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DATE, -1);
        // return calendar.getTime();
        return getEndDate(calendar.getTime());
    }

获取一天的结束时间==============================================================
    public static Date getEndDate(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

获取该月的头天或最后一天===========================================================
public static Date getMonthEnd(Date date) {
        Calendar clar = Calendar.getInstance();
        clar.setTime(date);
        clar.set(Calendar.YEAR, clar.get(Calendar.YEAR));
        clar.set(Calendar.DAY_OF_MONTH, clar.getActualMaximum(Calendar.DATE));//该月的最后一天
        clar.set(Calendar.DAY_OF_MONTH, clar.getActualMinimum(Calendar.DATE));//该月的头一天
        return clar.getTime();
    }

时间的获取处理================================================================
        Calendar beginCa = Calendar.getInstance();
        Date now = new Date();
        beginCa.setTime(now);
        System.out.println(bartDateFormat.format(beginCa.getTime()));
        beginCa.add(Calendar.YEAR, 5);
        System.out.println(bartDateFormat.format(beginCa.getTime()));
        now=beginCa.getTime();
        System.out.println(bartDateFormat.format(now));

匹配类型=====================================================================
        String s = "home-home-home-home-home-home";
        Pattern p = Pattern.compile("home");
        Matcher m = p.matcher(s);
        System.out.println("m.find()----------------------------");
        int j=0;
        while (m.find()) {// Matcher.find( )的功能是发现s里的,与pattern相匹配的多个字符序列。
            System.out.println(j+"**Match /"" + m.group() + "/" at positions "
                    + m.start() + "-" + (m.end() - 1));
            j++;
        }
        int i = 0;
        System.out
                .println("-------------");
        System.out.println("m.find(i)----------------------------");
        while (m.find(i)) {// Matcher.find(i)是带int参数的,它会告诉方法从哪里开始找——即从参数位置开始查找。
            System.out.println(i+"**Match /"" + m.group() + "/" at positions "
            + m.start() + "-" + (m.end() - 1));
            i++;
        }
        System.out
                .println("------------");
        System.out.println("m.matches()----------------------------");
        String s1 = "home-home";
        Pattern p1 = Pattern.compile("home-home");
        Matcher m1 = p1.matcher(s1);
        if (m1.matches()) {// matches( )的前提是Pattern匹配整个字符串
            System.out.println("Match /"" + m1.group() + "/" at positions "
                    + m1.start() + "-" + (m1.end() - 1));
        } else {
            System.out.println(m1.matches());
        }
        System.out
                .println("---------------");
        System.out.println("m.lookingAt()----------------------------");
        String s2 = "home-home";
        Pattern p2 = Pattern.compile("home");
        Matcher m2 = p2.matcher(s2);
        if (m2.lookingAt()) {// lookingAt( )的意思是Pattern匹配字符串的开头。
            System.out.println("Match /"" + m2.group() + "/" at positions "
                    + m2.start() + "-" + (m2.end() - 1));
        } else {
            System.out.println(m1.lookingAt());
        }
    }

文件的读取和写入================================================================
String s, a, b, c;
        HashMap m = new HashMap();
        String[] e;
        String[] f = new String[6];
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(
                "D:......read.in"));
        BufferedReader br = new BufferedReader(new InputStreamReader(in, "gbk"));
        BufferedInputStream in2 = new BufferedInputStream(new FileInputStream(
                "D:......read2.in"));
        BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
        BufferedWriter out = new BufferedWriter(
                new OutputStreamWriter(
                        new BufferedOutputStream(
                                new FileOutputStream(
                                        "D:......................write.out"))));
        while ((s = br.readLine()) != null) {
            out.write(s);
        }
        in.close();
        br.close();
        in2.close();
        br2.close();
        out.close();

浮点数设置小数位================================================================
private Double formatDouble(Double value) {
        DecimalFormat df = new DecimalFormat("#.##");// 按照指定格式截取,最多保留两位小数,截取方式是"四舍五入"
        if (value != null) {
            return Double.valueOf(df.format(value.doubleValue()));
        } else {
            return 0.0;
        }
    }
    private Double setDoubleDigit(Double value,int digit){
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(digit);
        return Double.valueOf(df.format(value));
    }
    public static void main(String[] args) {
        TestDoubleFormat testDoubleFormat = new TestDoubleFormat();
        DecimalFormat df = new DecimalFormat("#.##");// 按照指定格式截取,截取方式是"四舍五入"
        Double a = 99.1155896;
        System.out.println("a=" + df.format(a));
        System.out.println("testDoubleFormat.formatDouble(a)="
                + testDoubleFormat.formatDouble(a));//没有格式化的会用科学计数法显示
        System.out.println("testDoubleFormat.setDoubleDigit(a,3)="
                + testDoubleFormat.setDoubleDigit(a,3));//设置小数位
        a = testDoubleFormat.formatDouble(a);
        System.out.println("a=" + df.format(a));
    }
输出:
a=99.12
testDoubleFormat.formatDouble(a)=99.12
testDoubleFormat.setDoubleDigit(a,3)=99.116
a=99.12
改变table中的一列的显示数据=========================================================
//用来重新计算两个截取小数点后的相乘值
    private void castValue(final JTable jTable, int multiplicationColNum,
            final int fColNum, final int sColNum) {
        jTable.getColumnModel().getColumn(multiplicationColNum)
                .setCellRenderer(new DefaultTableCellRenderer() {
                    public Component getTableCellRendererComponent(
                            JTable table, Object value, boolean isSelected,
                            boolean hasFocus, int row, int column) {
                        super.getTableCellRendererComponent(table, value,
                                isSelected, hasFocus, row, column);
                        super.setText((value == null) ? "0"
                                : multiplicationValue(Double
                                        .parseDouble((String) value), jTable
                                        .getModel().getValueAt(row, fColNum),
                                        jTable.getModel().getValueAt(row,
                                                sColNum)));
                        return this;
                    }
                });
    }
 
 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值