All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyuncs.utils.ParameterHelper Maven / Gradle / Ivy

There is a newer version: 1.8.10
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.aliyuncs.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.SimpleTimeZone;
import java.util.UUID;

public class ParameterHelper {

	private final static String TIME_ZONE = "GMT";
	private final static String FORMAT_ISO8601 = "yyyy-MM-dd'T'HH:mm:ss'Z'";
	private final static String FORMAT_RFC2616 = "EEE, dd MMM yyyy HH:mm:ss zzz";
	
    public ParameterHelper() {
    }

    public static String getUniqueNonce() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }

    public static String getISO8601Time(Date date) {
    	Date nowDate = date;
    	if (null == date){
    		nowDate = new Date();
    	}
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_ISO8601);
        df.setTimeZone(new SimpleTimeZone(0, TIME_ZONE));
        
        return df.format(nowDate);
    }

    public static String getRFC2616Date(Date date) {
    	Date nowDate = date;
    	if (null == date){
    		nowDate = new Date();
    	}
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_RFC2616, Locale.ENGLISH);
        df.setTimeZone(new SimpleTimeZone(0, TIME_ZONE));
        return df.format(nowDate);
    }

    public static Date parse(String strDate) throws ParseException {
    	if (null == strDate || "".equals(strDate)){
    		return null;
    	}
        try {
        	return parseISO8601(strDate);
        } catch (ParseException exp) {
        	return parseRFC2616(strDate);
        }
    }
    
    public static Date parseISO8601(String strDate) throws ParseException {
    	if (null == strDate || "".equals(strDate)){
    		return null;
    	}
    	SimpleDateFormat df = new SimpleDateFormat(FORMAT_ISO8601);
    	df.setTimeZone(new SimpleTimeZone(0, TIME_ZONE));
        return df.parse(strDate);  
    }
    
    public static Date parseRFC2616(String strDate) throws ParseException {
    	if (null == strDate || "".equals(strDate) || strDate.length() != FORMAT_RFC2616.length()){
    		return null;
    	}
    	SimpleDateFormat df = new SimpleDateFormat(FORMAT_RFC2616, Locale.ENGLISH);
    	df.setTimeZone(new SimpleTimeZone(0, TIME_ZONE));
        return df.parse(strDate);  
    }
    
    public static String md5Sum(byte[] buff){
    	try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(buff);
            return Base64Helper.encode(messageDigest);
		} catch (Exception e) {
		}
       return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy