Java中常用的方法
来源:互联网 发布:类似于天下游的软件 编辑:程序博客网 时间:2024/05/19 00:15
格式时间================================================================ ==== 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.12testDoubleFormat.formatDouble(a)=99.12testDoubleFormat.setDoubleDigit(a,3)=99.116a=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; } }); }
- java中常用的方法
- Java中常用的方法
- Java中常用的方法
- JAVA中常用类的常用方法
- JAVA中常用类的常用方法
- java 中StringBuffer中常用的方法
- java 中常用方法
- java中常用方法
- java中常用的一些方法
- java中常用的一些方法
- java中常用的一些方法
- java 中常用的方法总结
- java 中常用的方法总结
- JAVA中File的常用方法
- java中常用的处理时间方法
- Java中常用的加密方法(JDK)
- Java中常用的加密方法(JDK)
- Java中常用的加密方法(JDK)
- 学会照顾同伴
- 为何我记不准Numeric 类型是什么
- php录入页面中动态从数据库中提取数据的实现
- 谷歌Google推出灾区亲人搜索,为抗震救灾服务
- (R)(彩虹连心 支援灾区)
- Java中常用的方法
- PHP与Javascript的两种交互方式
- mysql的grant语法
- 收起回忆,拥抱我的工作
- Tomcat双向SSL认证的配置
- 调查显示:开源性是PHP的优势所在(图表)
- poj数百篇解题报告
- 正则表达式教程-1
- XML的结构和语法