java中字符串日期类等处理

本文介绍了Java中的StringTokenizer类用于字符串分割的方法,并演示了不同分割选项的效果。此外,还探讨了DateFormat类在多线程环境下的正确使用方式,提供了一个线程安全的例子。最后,给出了在Java中获取当前路径及上级目录路径的示例。

1. StringTokenizer:

字符串分割类:

public class TestALL {
    public static void main(String[] args) {
        System.out.println("默认以空格,\\t,\\r,\\n分割");
        StringTokenizer st = new StringTokenizer("www ooobj com");
        while(st.hasMoreElements()){
            System.out.println("Token:" + st.nextToken());
        }
        System.out.println("指定以.分割");
        st = new StringTokenizer("www.ooobj.com",".");
        while(st.hasMoreElements()){
            System.out.println("Token:" + st.nextToken());
        }
        System.out.println("指定以.分割,并在结果中包含分隔符");
        st = new StringTokenizer("www.ooobj.com",".",true);
        while(st.hasMoreElements()){
            System.out.println("Token:" + st.nextToken());
        }
    }
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

输出:

默认以空格,\t,\r,\n分割
Token:www
Token:ooobj
Token:com
指定以.分割
Token:www
Token:ooobj
Token:com
指定以.分割,并在结果中包含分隔符
Token:www
Token:.
Token:ooobj
Token:.
Token:com
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2. DateFormat:

想必大家对SimpleDateFormat并不陌生。SimpleDateFormat 是 Java 中一个非常常用的类,该类用来对日期字符串进行解析和格式化输出,但如果使用不小心会导致非常微妙和难以调试的问题,因为 DateFormat 和 SimpleDateFormat 类不都是线程安全的,在多线程环境下调用 format() 和 parse() 方法应该使用同步代码来避免问题。 
最好的方法,使用ThreadLocal:

package com.peidasoft.dateformat;

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

public class ConcurrentDateUtil {

    private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    public static Date parse(String dateStr) throws ParseException {
        return threadLocal.get().parse(dateStr);
    }

    public static String format(Date date) {
        return threadLocal.get().format(date);
    }
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

3. Java获取路径:

public String getCurrentPath(){  
       //取得根目录路径  
       String rootPath=getClass().getResource("/").getFile().toString();  
       //当前目录路径  
       String currentPath1=getClass().getResource(".").getFile().toString();  
       String currentPath2=getClass().getResource("").getFile().toString();  
       //当前目录的上级目录路径  
       String parentPath=getClass().getResource("../").getFile().toString();  

       return rootPath;         

   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值