Java正確獲取星期Calendar.DAY_OF_WEEK
http://chamcon.iteye.com/blog/2144433html
Java SimpleDateFormat 中英文時間格式化轉換java
https://blog.csdn.net/KingWTD/article/details/48089111api
java-使用SimpleDateFormat格式化日期
http://zy-email1991.iteye.com/blog/2243021服務器
編寫一個java程序用以將AM/PM格式的時間轉換爲24小時格式
https://zhidao.baidu.com/question/1798461878119559747.html?qbl=relate_question_1&word=java%20am%20pm%20%D3%A2%CE%C4spa
我的寫的一個Demo:.net
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.TimeZone; public class Main { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(TimeZone.getDefault()); // FAQ : // 1.不一樣地區國家時間格式顯示和同步問題? UTC,格林威治?夏威夷?國際化 ~ locale // 2.本地client和服務器時間同步問題,好比定時下發執行任務,本地預約時早於服務器當前時間,則沒法執行 System.out.println("\nTimezone: " + TimeZone.getDefault()); long time = calendar.getTimeInMillis(); Date date1 = calendar.getTime(); Date date2 = new Date(); // get current date // method1: direct format fun call // a ~ AM/PM, 上午/下午 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // HH ~ 24小時制 System.out.printf("\nTime1 in 24h : " + sdf1.format(calendar.getTime())); SimpleDateFormat sdf11 = new SimpleDateFormat("yyyy-MM-dd hh:mm"); // hh ~ 12小時制 System.out.printf("\nTime11 in 12h : " + sdf11.format(calendar.getTime())); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd a HH:mm", Locale.ENGLISH); System.out.printf("\nTime2: " + sdf2.format(calendar.getTime())); SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy年MM月dd日HH:mm"); System.out.printf("\nTime3: " + sdf3.format(calendar.getTime())); SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy年MM月dd日 a HH:mm"); System.out.printf("\nTime4: " + sdf4.format(calendar.getTime())); // method2: use self define format func int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); // 歐美國家一週的第一天是星期天,這在java api代碼中是寫死的,所以這裏要本身手動作下判斷 // Calendar中的星期1、2、3、4、5、6、日、DAY_OF_WEEK等都是以一個常量的形式存在的, // 咱們設置setFirstDayOfWeek()並不能改變這些常量的值 int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); // 獲取當天是星期幾 boolean isFirstSunday = (calendar.getFirstDayOfWeek() == Calendar.SUNDAY); if (isFirstSunday) { dayOfWeek -= 1; if (dayOfWeek == 0) { dayOfWeek = 7; } } int dayOfWeekInMonth = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH); // 某月中第幾周. 按這個月1號算,1號起就是第1周,8號起就是第2周 int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH); // 該月的第幾周 int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR); // 一年的第幾天,從1號算起 System.out.println("\n" + getFormattedTime(year, month, day, hour, minute)); System.out.println("\ndayOfWeek: " + dayOfWeek + "\ndayOfWeekInMonth: " + dayOfWeekInMonth + "\ndayOfYear: " + dayOfYear); } /** * 格式化日期字符串,形如 xx年xx月xx日xx:xx:xx * @param year * @param month * @param day * @param hour * @param minute * @return String */ public static String getFormattedTime(int year, int month, int day, int hour, int minute) { String yearStr = ""; String monthStr = ""; String dayStr = ""; String hourStr = ""; String minuteStr = ""; // year if (year < 10) { yearStr = "0" + year; } else { yearStr = String.valueOf(year); } // month if (month < 10) { monthStr = "0" + month; } else { monthStr = Integer.toString(month); } // day if (day < 10) { dayStr = "0" + day; } else { dayStr = Integer.toString(day); } // hour if (hour < 10) { hourStr = "0" + hour; } else { hourStr = Integer.toString(hour); } // minute if (minute < 10) { minuteStr = "0" + minute; } else { minuteStr = Integer.toString(minute); } return yearStr + "年" + monthStr + "月" + dayStr + "日" + hourStr + ":" + minuteStr; } }