日期转化为时间戳,英文的日期转为日期格式

该文章介绍怎么将中文年月日的日期转为时间戳,英文格式的时间转为中文日期的yyyy-MM-dd格式,然后调用方法再转为时间戳

在项目开发中有用到所以记录下来,转化给日期格式之后转为时间戳

    // 中文日期时间戳  传参2009年6月13日 12:28
    // 也可以将英文日期转为时间戳将日期格式改为英文的就可以了
    private static String getTime(String pubdate) {
        // TODO Auto-generated method stub
        String re_time = null;
        //日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        Date d;
        try {
            d = sdf.parse(pubdate);
            long l = d.getTime();
            String str = String.valueOf(l);
            re_time = str.substring(0, 10);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return re_time;
    }



// 英文日期转中文日期
    // "Saturday, June 13, 2009 at 2:28pm"转化成"2009年6月13日 12:28"
    public static String date2String(String date) {

        StringBuilder result = new StringBuilder();
        String year = "";
        String month = "";
        String day = "";
        String time = "";

        String[] timeStr = date.split(", ");

        for (int i = timeStr.length - 1; i > 0; i--) {
            // System.out.println(timeStr[i]);
            if (i == 2) {
                String[] yearAndTime = timeStr[i].split(" at ");
                year = yearAndTime[0];
                time = yearAndTime[1].contains("am") ? yearAndTime[1].replace("am", "")
                        : new StringBuilder("1").append(yearAndTime[1].replace("pm", "")).toString();
            } else if (i == 1) {
                String[] monthAndDay = timeStr[i].split(" ");
                day = monthAndDay[1];

                switch (monthAndDay[0]) {
                case "January":
                    month = "1";
                    break;
                case "February":
                    month = "2";
                    break;
                case "March":
                    month = "3";
                    break;
                case "April":
                    month = "4";
                    break;
                case "May":
                    month = "5";
                    break;
                case "June":
                    month = "6";
                    break;
                case "July":
                    month = "7";
                    break;
                case "August":
                    month = "8";
                    break;
                case "September":
                    month = "9";
                    break;
                case "October":
                    month = "10";
                    break;
                case "November":
                    month = "11";
                    break;
                case "December":
                    month = "12";
                    break;
                }
            }
        }
        result.append(year).append("年").append(month).append("月").append(day).append("日 ").append(time);
        return result.toString();
    }

你可能感兴趣的:(java)