zoukankan      html  css  js  c++  java
  • Java 日期时间获取和显示

    Java正确获取星期Calendar.DAY_OF_WEEK
    http://chamcon.iteye.com/blog/2144433

    Java SimpleDateFormat 中英文时间格式化转换

    https://blog.csdn.net/KingWTD/article/details/48089111

    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%C4

    个人写的一个Demo:

    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("
    Timezone: " + 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("
    Time1 in 24h : " + sdf1.format(calendar.getTime()));
    
            SimpleDateFormat sdf11 = new SimpleDateFormat("yyyy-MM-dd hh:mm"); // hh ~ 12小时制
            System.out.printf("
    Time11 in 12h : " + sdf11.format(calendar.getTime()));
            
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd a HH:mm", Locale.ENGLISH);
            System.out.printf("
    Time2: " + sdf2.format(calendar.getTime()));
            
            SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy年MM月dd日HH:mm");
            System.out.printf("
    Time3: " + sdf3.format(calendar.getTime()));
            
            SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy年MM月dd日 a HH:mm");
            System.out.printf("
    Time4: " + 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中的星期一、二、三、四、五、六、日、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("
    " + getFormattedTime(year, month, day, hour, minute));
            System.out.println("
    dayOfWeek: " + dayOfWeek + "
    dayOfWeekInMonth: " + dayOfWeekInMonth 
                    + "
    dayOfYear: " + 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;
        }
    }

  • 相关阅读:
    DES加密
    隐写术-隐藏明文的另类办法
    古典加密方法(三)转轮机
    古典加密方法(二)置换技术
    古典加密方法(一)代换技术
    基于加密算法的攻击类型的通俗解释(转载自知乎)
    指针小结
    幸福是什么?
    Jmeter常用脚本开发之FTP请求
    python自动发邮件总结及实例说明
  • 原文地址:https://www.cnblogs.com/bluestorm/p/8923814.html
Copyright © 2011-2022 走看看