// 根据用户给出的userStr,以及格式化字符formatStr,返回一个Date变量
Date my_StrConvert(string userStr, string formatStr)
// 根据给定的格式化字符formatStr,输出当前时间。
String GetCurrentDateTimeStr(string formatStr);
import java.util.*;
import java.io.*;
import java.text.*;
public class app1 {
public static void main(String[] args)
{
System.out.println("输入格式化字符formatStr:");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine();//输入格式化字符时,有空格,则不能使用Scanner输入,则使用BufferedReader输入
System.out.println("当前时间是"+GetCurrentDateTimeStr(s));
}catch(IOException e) {}
System.out.println( my_StrConvert("2018/02/15 15:11:23","yyyy/MM/dd HH:mm:ss"));
}
public static Date my_StrConvert(String userStr, String formatStr)
{//根据用户给出的userStr,以及格式化字符formatStr,返回一个Date变量
SimpleDateFormat f2 = new SimpleDateFormat(formatStr);//格式化字符串限制转化的年月日时分秒参数
try {
Date date3 = f2.parse(userStr);//将字符串时间转化为Date类
return date3;
} catch (ParseException e) {}
return null;
}
public static String GetCurrentDateTimeStr(String formatStr)
{// 根据给定的格式化字符formatStr,输出当前时间
SimpleDateFormat f1 = new SimpleDateFormat(formatStr);
return f1.format(new Date());
}
}