Java时间日期格式
public class StreamUtil {
/*
- 方法1:批量关闭流,参数能传入无限个。
- 例如传入FileInputStream对象、JDBC中Connection对象都可以关闭,并且参数个数不限。
*/
public static void closeAll(AutoCloseable src) throws IOException {
try {
src.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
- 方法2:传入一个文本文件对象,默认为UTF-8编码,返回该文件内容,要求方法内部调用上面第1个方法关闭流
*/
public static String readTextFile(InputStream src) {
BufferedReader reader = new BufferedReader(new InputStreamReader(src, StandardCharsets.UTF_8));
byte b[] = new byte[2048];
int len = 0;
int temp = 0;
String stu = “”;
try {
while ((temp = reader.read()) != -1) {
b[len] = (byte) temp;
len++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
closeAll(src);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new String(b, 0, len);
}
}
/*
- 方法3:传入文本文件对象,返回该文件内容,并且要求内部调用上面第2个方法。
*/
@SuppressWarnings({ “resource”, “finally” })
public static String readTextFile(File txtFile) {
String file = “”;
try {
FileInputStream inputStream = new FileInputStream(txtFile);
file = readTextFile(inputStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
return file;
}
}
//File f 文件目录,rootPath 根目录,outPath输出目录,suffix输出文件的后缀名
public static void CopyToPath(File f, String rootPath, String outPath, String suffix) {
File[] files = f.listFiles();
for (File file : files) {
//输出流
OutputStreamWriter writer = null;
//获取当前文件在目录下的路径
String path = file.getPath();
//截取根目录后面的字符串
String substring = path.substring(rootPath.length());
//将输出目录和文件路径拼接,生成新的路径
File file2 = new File(outPath + substring);
//判断是否文件
if (!file.isDirectory() && file != null) {
//获取文件后缀名
String string = FileUtil.getExtendName(file.getName());
//判断是否是要输出的文件
if (string.equals(suffix)) {
//读取当前文件内容
String string2 = StreamUtil.readTextFile(file);
try {
//获取输出流
writer = new OutputStreamWriter(new FileOutputStream(file2));
//将文件内容,输出到指定地址
writer.write(string2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//关闭流
} else {
continue;
}
} else {
//判断输出目录是否存在,不存在则创建
if (!file2.exists()) {
file2.mkdirs();
}
//判断时目录,则递归
CopyToPath(file, rootPath, outPath, suffix);
System.out.println(“递归中”);
}
}
}
}
public class DateUtil {
//验证字符串是否为日期,如果是日期,则转换为时间格式
public static Map<String, Object> StringTrunDate(String stu) {
//为保存验证结果,所以使用map类型
Map<String, Object> map = new HashMap<String, Object>();
if (stu.length() < 11) {
//当日期类型为年月日时
SimpleDateFormat format = new SimpleDateFormat(“yyyy/MM/dd”);
try {
//将字符串转换为日期
Date date = format.parse(stu);
map.put("flag", true);
map.put("Date", date);
return map;
} catch (ParseException e) {
// TODO Auto-generated catch block
//保存错误信息
map.put("flag", false);
}
} else if (stu.length() < 20) {
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
//将字符串转换为日期
Date date = format.parse(stu);
map.put("flag", true);
map.put("Date", date);
return map;
} catch (ParseException e) {
// TODO Auto-generated catch block
//保存错误信息
map.put("flag", false);
}
}
//当不字符串不是日期类型时,返回false
map.put("flag", false);
return map;
}
/*
- 方法1:
*/
public static Date getDateByInitMonth(Date src) {
//TODO 实现代码
//设置天
src.setDate(1);
src.setHours(0);//时
src.setMinutes(0);//分
src.setSeconds(0);//秒
return src;
}
/*
- 方法2:
*/
public static Date getDateByFullMonth(Date src) {
//验证是否为2月
if (src.getMonth() == 2) {
//判断是否为闰年
if (src.getYear() % 4 == 0 && src.getYear() % 100 != 0) {
src.setDate(28);
} else {
src.setDate(29);
}
//判断是否为小月
} else if (src.getMonth() == 4 || src.getMonth() == 6 || src.getMonth() == 9 || src.getMonth() == 11) {
src.setDate(30);
//条件都不满足则为大月
} else {
src.setDate(31);
}
//设置小时
src.setHours(23);
//设置分
src.setMinutes(59);
//设置秒
src.setSeconds(59);
return src;
//TODO 实现代码
}
}
================================================================================================
public class FileUtil {
/*
- 方法1:给定一个文件名,返回该文件名的扩展名,例如“aaa.jpg”,返回“.jpg”
*/
public static String getExtendName(String fileName) {
//从后查找’.’
int i = fileName.lastIndexOf(".");
//以查找到的点为起始,截取后面的字符串
String string = fileName.substring(i);
return string;
//TODO 实现代码
}
}