码迷,mamicode.com
首页 > 其他好文 > 详细

常用工具类

时间:2018-04-03 20:11:18      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:simple   core   字符串数组   获取   字段   日历   特定   线程   ati   

字符串连接

方法1:使用“+”

int score=80;
String info="成绩"+score;

方法2:使用String类的concat()方法

String s=new String("你好, ");
String name=new String("张三!");
String sentence=s.concat(name);
System.out.println(sententce);

String类提供了split()方法,将一个字符串分割为子字符串,结果作为字符串数组返回。

同时,concat()方法支持把指定字符附加到字符串末尾。

字符串常用提取方法

public int indexOf(int ch)   
public int indexOf(String value)   //搜索第一个出现的字符ch

public int lastIndexOf(int ch)
public int lastIndexOf(String value) //搜索最后一个出现的字符ch(或字符串value)

public String substring(int index) //提取从位置索引开始的字符串部分
public String substring(int beginindex,int endindex) //提取beginindex和endindex之间的字符串部分
public String trim() //返回一个前后不含任何空格的调用字符串的副本

StringBuffer :String增强版

定义:带缓冲区的String类,支持动态调整字符串长度,是String的增强版。线程安全类。

对字符串频繁修改(如字符串连接)时,使用StringBuffer类可以大大提高程序执行效率

StringBuffer声明

StringBuffer sb=new StringBuffer();
StringBuffer sb=new StringBuffer("aaa");

StringBuffer的使用

sb.toString();       //转化为String类型
sb.append();       //追加字符串
sb.insert(1,"**");   //插入字符串

 StringBuilder类

该类被设计用作 StringBuffer 的一个简易替换,非线程安全类,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快。

操作日期时间

类 Date 表示特定的瞬间,精确到毫秒。

时间戳:时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。

Date类的gettime()获得时间戳:

Date date = new Date();
long times=date.getTime();

SimpleDateFormat类:用于定制日期时间的格式

Date date =new Date();//创建日期对象
SimpleDateFormat formater=new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);//定制日期格式
String now =formater.format(data);
System.out.println(now);

Calendar类

是一个抽象类,用于设置和获取日期/时间数据的特定部分

Calendar类提供一些方法和静态字段来操作日历

方法或属性      说明

int get(int field)        返回给定日历字段的值

MONTH         指定月

DAY_OF_MONTH   指示一个月中的某天

DAY_OF_WEEK      指示一个星期中的某天

实例:对比String和StringBuffer的效率

    //定义一个字符串变量
        String str="早上好";
        //获得当前系统的时间
        long start=System.currentTimeMillis();
        //循环拼接字符串
        for (int i = 0; i < 10000; i++) {
            str+="吃了吗?";
        }
        long end=System.currentTimeMillis();
        //执行一万次拼接字符串需要的时间
        System.out.println("String拼接需要的时间="+(end-start));
    }
    
    public static void main(String[] args) {
        aaa();
        //先创建一个对象
        StringBuffer buffer=new StringBuffer("早上好");
        //获得当前系统的时间
                long start=System.currentTimeMillis();
                //循环拼接字符串
                for (int i = 0; i < 10000; i++) {
                    buffer.append("吃了吗?");
                }
                long end=System.currentTimeMillis();
                //执行一万次拼接字符串需要的时间
                System.out.println("StringBuffer拼接需要的时间="+(end-start));
    }

文件操作

文件的定义:文件可认为是相关记录或放在一起的数据的集合。文件一般保存在硬盘、U盘、光盘、云盘的媒介中。

 JAVA API :java.io.File 类

 File对象构建

File file = new File( String pathname );
//String pathname格式:"e:\\test .txt"或"e:/test .txt"

File类常用方法

boolean exists( )判断文件或目录是否存在

boolean isFile( )判断是否是文件

boolean isDirectory( )判断是否是目录

String getPath( )返回此对象表示的文件的相对路径名

String getAbsolutePath( )返回此对象表示的文件的绝对路径名

String getName( )返回此对象表示的文件或目录的名称

boolean delete( )删除此对象指定的文件或目录

boolean createNewFile( )创建名称的空文件,不创建文件夹

long length()返回文件的长度,单位为字节, 如果文件不存在,则返回 0L

 

常用工具类

标签:simple   core   字符串数组   获取   字段   日历   特定   线程   ati   

原文地址:https://www.cnblogs.com/sonder/p/8710782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!