Tukar rentetan kepada tarikh yang sepadan
Date date = simpleDateFormat.parse(string);
Tukar tarikh kepada rentetan
String string = simpleDateFormat.format(date);
Nota bahawa pengecualian akan dilemparkan kerana format yang mungkin ditakrifkan tidak sepadan dengan format yang disediakan oleh rentetan sebenar.
Tukar tarikh aksara Cina tahun, bulan dan hari kepada tarikh dengan - - pemisah
public static void main(String[] args) throws ParseException { //统一日期格式 String StrVal = "2018年05月22日"; Date d1 = new SimpleDateFormat("yyyy年MM月dd日").parse(StrVal); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String time = format.format(d1); System.out.println(time); }
/** * 将日期对象格式化为指定格式的日期字符串 * @param date 传入的日期对象 * @param format 格式 * @return */ public static String formatDate(Date date,String format){ String result=""; SimpleDateFormat sdf=new SimpleDateFormat(format); if(date!=null){ result=sdf.format(date); } return result; } /** * 将日期字符串转换成一个日期对象 * @param dateStr 日期字符串 * @param format 格式 * @return * @throws ParseException */ public static Date formatDate(String dateStr,String format) throws ParseException{ SimpleDateFormat sdf=new SimpleDateFormat(format); return sdf.parse(dateStr); } public static void main(String[] args) throws ParseException { Date date=new Date(); System.out.println(formatDate(date,"yyyy-MM-dd")); System.out.println(formatDate(date,"yyyy-MM-dd HH:mm:ss")); System.out.println(formatDate(date,"yyyy年MM月dd日HH时mm分ss秒")); String dataStr="1989-11-02 18:01:41"; Date date2=formatDate(dataStr,"yyyy-MM-dd HH:mm:ss"); System.out.println(formatDate(date2,"yyyy-MM-dd HH:mm:ss")); }
Jalankan output:
2016-11 -02
2016-11-02 18:06:50
November 02, 2016 18:06:50
1989-11-02 18:02-1989 :41
Kelas SimpleDateFormat digunakan terutamanya untuk penukaran jenis tarikh, pemformatan tarikh yang biasa digunakan
public static void main(String[] args) { //默认输出格式 Date date=new Date(); System.out.println(date); //日期格式化显示,首先定义格式 SimpleDateFormat sdf1=new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf3=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdf4=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒"); //将格式应用于日期 System.out.println(sdf1.format(date)); System.out.println(sdf2.format(date)); System.out.println(sdf3.format(date)); System.out.println(sdf4.format(date)); }
Kalendar mempunyai DAY_OF_WEEK yang boleh mengembalikan hari dalam seminggu; >Kendalikan kalendar
import java.util.Calendar; public class Test { public static void main(String[] args) { String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; Calendar calendar=Calendar.getInstance(); System.out.println("今天是"+weekDays[calendar.get(Calendar.DAY_OF_WEEK)-1]); } }
Dapatkan masa
Biasa digunakan untuk menetapkan nilai tahun, bulan, hari, jam, nilai 6 minit dan saat, sila ambil perhatian bahawa subskrip bulan bermula dari 0, jadi untuk mendapatkan bulan, +1 Tetapkan pada tarikh tertentu, anda boleh menetapkan tahun, bulan, hari, jam, minit dan saat pada satu masa Sejak langganan bulan bermula dari 0, bulan perlu -1 public static void main(String[] args) {
//默认输出格式
Date now = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String time = format.format(now);
System.out.println(time);
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.add(Calendar.MONTH, 1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String nowTime = sdf.format(calendar.getTime());
System.out.println(nowTime);
calendar.add(Calendar.MONTH, -2);
String nowTime2 = sdf.format(calendar.getTime());
System.out.println(nowTime2);
}
Pengiraan masa
public static void main(String[] args) { //默认输出格式 Date now = new Date(); SimpleDateFormat nowSdf = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(nowSdf.format(now)); Calendar calendar = Calendar.getInstance(); // 赋值时年月日时分秒常用的6个值,注意月份下标从0开始,所以取月份要+1 System.out.println("年:" + calendar.get(Calendar.YEAR)); System.out.println("月:" + (calendar.get(Calendar.MONTH) + 1)); System.out.println("日:" + calendar.get(Calendar.DAY_OF_MONTH)); System.out.println("时:" + calendar.get(Calendar.HOUR_OF_DAY)); System.out.println("分:" + calendar.get(Calendar.MINUTE)); System.out.println("秒:" + calendar.get(Calendar.SECOND)); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String nowTime2 = sdf.format(calendar.getTime()); System.out.println(nowTime2); }
Pengiraan tarikh
Dapatkan bilangan hari minimum dan maksimum dalam bulan ini
public static void main(String[] args) { //默认输出格式 Date now = new Date(); SimpleDateFormat nowSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(nowSdf.format(now)); Calendar calendar = Calendar.getInstance(); calendar.set(2013, 5, 4, 13, 44, 51); calendar.set(Calendar.YEAR, 2014);//年 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowTime = sdf.format(calendar.getTime()); System.out.println(nowTime); calendar.set(Calendar.MONTH, 7);//月(月份0代表1月) SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowTime1 = sdf1.format(calendar.getTime()); System.out.println(nowTime1); calendar.set(Calendar.DATE, 11);//日 calendar.set(Calendar.HOUR_OF_DAY, 15);//时 calendar.set(Calendar.MINUTE, 33);//分 calendar.set(Calendar.SECOND, 32);//秒 }
Dapatkan Isnin minggu ini, Isnin minggu lepas, Isnin minggu ini
public static void main(String[] args) { //默认输出格式 Date now = new Date(); SimpleDateFormat nowSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(nowSdf.format(now)); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(Calendar.YEAR, 1);//年 calendar.add(Calendar.MONTH, 1);//月 calendar.add(Calendar.DATE, 1);//日 //calendar.add(Calendar.DAY_OF_YEAR, 1);//今年的第 N 天 //calendar.add(Calendar.DAY_OF_MONTH, 1); // 本月第 N 天 //calendar.add(Calendar.DAY_OF_WEEK, 1);// 本周几 calendar.add(Calendar.HOUR_OF_DAY, -1);//时 calendar.add(Calendar.MINUTE, 1);//分 calendar.add(Calendar.SECOND, 1);//秒 //calendar.add(calendar.WEEK_OF_MONTH, 1);//增加一个礼拜 //calendar.add(calendar.WEEK_OF_YEAR,1);//增加一个礼拜 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowTime1 = sdf1.format(calendar.getTime()); System.out.println(nowTime1); }
Pengiraan hari
Kelas Penggunaan Tarikh untuk mengira perbezaan tarikh
public static void main(String[] args) { //默认输出格式 Date now = new Date(); SimpleDateFormat nowSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar calendar = Calendar.getInstance(); int firstD = calendar.getActualMinimum(calendar.DAY_OF_MONTH); int lastD = calendar.getActualMaximum(calendar.DAY_OF_MONTH); System.out.println("获取本月的第一天和最后天:" + firstD +"," + lastD); }
public static Date geLastWeekMonday(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(getThisWeekMonday(date)); cal.add(Calendar.DATE, -7); return cal.getTime(); } public static Date getThisWeekMonday(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); // 获得当前日期是一个星期的第几天 int dayWeek = cal.get(Calendar.DAY_OF_WEEK); if (1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 cal.setFirstDayOfWeek(Calendar.MONDAY); // 获得当前日期是一个星期的第几天 int day = cal.get(Calendar.DAY_OF_WEEK); // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值 cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day); return cal.getTime(); } public static Date getNextWeekMonday(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(getThisWeekMonday(date)); cal.add(Calendar.DATE, 7); return cal.getTime(); } public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = sdf.parse("2017-09-10"); System.out.println("今天是" + sdf.format(date)); System.out.println("上周一" + sdf.format(geLastWeekMonday(date))); System.out.println("本周一" + sdf.format(getThisWeekMonday(date))); System.out.println("下周一" + sdf.format(getNextWeekMonday(date))); } catch (Exception e) { e.printStackTrace(); } }Kelas alat tarikh
public static void main(String[] args) { Calendar love = Calendar.getInstance(); Calendar now = Calendar.getInstance(); love.set(2016, 8, 6); //真实的日期是2016-9-6; int days = (int) ((now.getTimeInMillis() - love.getTimeInMillis()) / (24 * 60 * 60 * 1000.0)); System.out.println(days); }rrree
Atas ialah kandungan terperinci Apakah kaedah untuk menulis kelas alat tarikh Java?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!