JavaSE-32 工具类及其他

日期转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.etc.util;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
/** 日期转换工具类 */
public class DateConvert {
 
    /** 日期对象转Long类型时间戳 */
    public static Long date2Long(Date date) {
        return date.getTime();
    }
 
    /** 时间戳转Date日期类型 */
    public static Date long2Date(Long timeStamp) {
        return new Date(timeStamp);
    }
 
    /** 日期转字符串 */
    public static String date2String(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }
 
    /** 字符串转日期 */
    public static Date string2Date(String dateStr, String pattern) {
        // String dateStr="2018-3-15 14:26:00";
        // String pattern="yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
 
    /** Long时间戳转日期字符串 */
    public static String long2String(Long timeStamp) {
        Date date = new Date(timeStamp);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
 
    /** 日期字符串转Long时间戳 */
    public static Long string2Long(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }
}

  

翻页类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.etc.util;
 
import java.util.List;
 
/**分页工具类*/
public class Page<T> {
     // 总页数
    private int totalPageCount = 1;
    // 页面大小,即每页显示记录数
    private int pageSize = 0;
    // 记录总数
    private int totalCount = 0;
    // 当前页号
    private int currPageNo = 1;
    // 每页数据集合
    private List<T> list;
  
    public List<T> getList() {
        return list;
    }
  
    public void setList(List<T> list) {
        this.list = list;
    }
  
    public int getCurrPageNo() {
        if (totalPageCount == 0)
            return 0;
        return currPageNo;
    }
  
    public void setCurrPageNo(int currPageNo) {
        if (this.currPageNo > 0)
            this.currPageNo = currPageNo;
    }
  
    public int getTotalPageCount() {
        return totalPageCount;
    }
  
    public void setTotalPageCount(int totalPageCount) {
        this.totalPageCount = totalPageCount;
    }
  
    public int getPageSize() {
        return pageSize;
    }
  
    public void setPageSize(int pageSize) {
        if (pageSize > 0)
            this.pageSize = pageSize;
    }
  
    public int getTotalCount() {
        return totalCount;
    }
  
    public void setTotalCount(int totalCount) {
        if (totalCount > 0) {
            this.totalCount = totalCount;
            // 计算总页数
            totalPageCount = this.totalCount % pageSize == 0 ? (this.totalCount / pageSize)
                    : this.totalCount / pageSize + 1;
        }
    }
}

  

读取配置文件

1
文件位置:src\database.properties
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.etc.util;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
/** 读取配置文件工具类 */
public class ConfigManager {
    private static ConfigManager configManager;
    private static Properties properties;
 
    // 构造工具类时,读取配置文件
    private ConfigManager() {
        String configFile = "database.properties";
        properties=new Properties();
        InputStream in=ConfigManager.class.getClassLoader().getResourceAsStream(configFile);
        try {
            properties.load(in);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
     
    //单例:获取实例
    public static ConfigManager getInstance(){
        if(configManager==null){
            configManager=new ConfigManager();
        }
        return configManager;
    }
     
    //通过key获取value
    public String getString(String key){
        return properties.getProperty(key);
    }
 
}

  

posted @   rask  阅读(165)  评论(0编辑  收藏  举报
(评论功能已被禁用)
点击右上角即可分享
微信分享提示