为什么 01-26-2018 日期,打印出来变成了2020年了?
打印:
java日期时间转换工具类
1.实现日期格式和类型转换
2.获得星期,时,分,秒
3.日期比较
4.生成账号和流水号
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;
/**
*/
public class DateUtil {
public static String FILE_NAME = "MMddHHmmssSSS";
public static String DEFAULT_PATTERN = "yyyy-MM-dd";
public static String DIR_PATTERN = "yyyy/MM/dd/";
public static String TIMESTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static String TIMES_PATTERN = "HH:mm:ss";
public static String NOCHAR_PATTERN = "yyyyMMddHHmmss";
/**
*
*
*/
public static String formatDefaultFileName() {
return formatDateByFormat(new Date(), FILE_NAME);
}
/**
*
*/
public static String formatDateByFormat(Date date, String format) {
String result = "";
if (date != null) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
result = sdf.format(date);
} catch (Exception ex) {
ex.printStackTrace();
}
}
return result;
}
/**
*
*
*/
public static String formatDefaultDate(Date date) {
return formatDateByFormat(date, DEFAULT_PATTERN);
}
/**
*
*
*/
public static String formatDirDate(Date date) {
return formatDateByFormat(date, DIR_PATTERN);
}
/**
*
*
*/
public static String formatTimesTampDate(Date date) {
return formatDateByFormat(date, TIMESTAMP_PATTERN);
}
/**
*
*
*/
public static String formatTimesDate(Date date) {
return formatDateByFormat(date, TIMES_PATTERN);
}
/**
*
*
*/
public static String formatNoCharDate(Date date) {
return formatDateByFormat(date, NOCHAR_PATTERN);
}
/**
*
*/
public static Date parseDate(String strDate, String pattern) {
try {
SimpleDateFormat format = new SimpleDateFormat(pattern);
Date nowDate = format.parse(strDate);
return nowDate;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
*
*
*
*/
public static Date parseDefaultDate(String date) {
return parseDate(date, DEFAULT_PATTERN);
}
/**
*
*
*
*/
public static Date parseTimesTampDate(String date) {
return parseDate(date, TIMESTAMP_PATTERN);
}
/**
*
*/
public static Date getCurrentDate() {
Calendar calendar = Calendar.getInstance();
return calendar.getTime();
}
/**
*
*/
public static Date parseUtilDate(java.sql.Date date) {
return date;
}
/**
*
*/
public static java.sql.Date parseSqlDate(Date date) {
return new java.sql.Date(date.getTime());
}
/**
*
*
*/
public static int getYear(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.YEAR);
}
/**
*
*
*/
public static int getMonth(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.MONTH) + 1;
}
/**
*
*
*/
public static int getWeek(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
dayOfWeek = dayOfWeek - 1;
if (dayOfWeek == 0) {
dayOfWeek = 7;
}
return dayOfWeek;
}
/**
*
*
*/
public static int getDay(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.DAY_OF_MONTH);
}
/**
*
*
*/
public static int getHour(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.HOUR_OF_DAY);
}
/**
*
*
*/
public static int getMinute(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.MINUTE);
}
/**
*
*
*/
public static int getSecond(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.SECOND);
}
/**
*
*
*/
public static long getMillis(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
/**
*
*
*
*/
public static Date addDate(Date date, int day) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTimeInMillis(getMillis(date) + ((long) day) 24 3600 * 1000);
return c.getTime();
}
/**
*
*
*
*/
public static int diffDate(Date date, Date date1) {
return (int) ((getMillis(date) - getMillis(date1)) / (24 3600 1000));
}
/**
*
*/
public static Long diffDateTime(Date date, Date date1) {
return (Long) ((getMillis(date) - getMillis(date1)) / 1000);
}
private static String[] randomValues = new String[] { "0", "1", "2", "3",
"4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "u", "t", "s", "o", "x", "v",
"p", "q", "r", "w", "y", "z" };
public static String getRandomNumber(int lenght) {
StringBuffer str = new StringBuffer();
for (int i = 0; i < lenght; i++) {
Double number = Math.random() * (randomValues.length - 1);
str.append(randomValues[number.intValue()]);
}
return str.toString();
}
/**
*
*/
public static String nextAcounnt(String acount) {
String newAcc = "";
if (Integer.parseInt(acount) < 10000) {
Integer newAc = Integer.parseInt(acount) + 1;
if (newAc < 1000) {
int count = String.valueOf(newAc).length();
if (count == 1) {
newAcc = "000" + newAc;
} else if (count == 2) {
newAcc = "00" + newAc;
} else if (count == 3) {
newAcc = "0" + newAc;
}
} else {
newAcc = String.valueOf(newAc);
}
} else {
newAcc = acount;
}
return newAcc;
}
public static boolean isNumeric1(String str) {
if (str != null && !"".equals(str) && str.length()<=9) {
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
} else {
return false;
}
}
/**
*
*/
public static String getSequenceNumber(int t) {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
String str = sdf.format(d);
String haomiao = String.valueOf(System.nanoTime());
str = str + haomiao.substring(haomiao.length() - 6, haomiao.length());
return str.substring(str.length() - t, str.length());
}
格式和你的字符串对应错了。应该是MM-dd-yyyy。编程是个细心的活。
String dateStr="01-26-2018" // 月-日-年
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); // 注意这个格式对应
try{
System.out.println(sdf.parse(dateStr));
} catch(ParseException e) {
e.printStackTrace();
}
15 回答8.4k 阅读
8 回答6.2k 阅读
1 回答4k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
2 回答3.1k 阅读
2 回答3.8k 阅读
3 回答1.7k 阅读✓ 已解决
你的日期格式是MM-dd-yyyy