I have this 开发者_运维百科function below.
It recieves a string and a key made out of another string.
The function takes the inputs
and adds on the date to make the exact same key to validate.
public bool isSecureKeyCorrect(string inputs,string thatKey)
{
DateTime now = DateTime.UtcNow.AddHours(2);
string currentDateString = (now.ToString("yyyyMMddHH"));
string year= currentDateString.Substring(0, 4);
string month = currentDateString.Substring(4, 2);
string day = currentDateString.Substring(6, 2);
string hour = currentDateString.Substring(8, 2);
string thisKey;
thisKey = inputs.Substring(0, 2) + month+ hour +
inputs.Substring(inputs.Length - 2, 2) + year + day;
if (thisKey == thatKey)
{
return true;
}
else
return false;
}
Now, since i'm a complete newb at java, and i need to make an equivalent to this function in java aswell, and i have very little knowledge about how Date
or DateTime
works in java, i'll be very happy if someone could give me some pointers how to properly adjust the code.
Thanks in advance.
Lookup the methods in GregorianCalendar: http://download.oracle.com/javase/6/docs/api/java/util/GregorianCalendar.html (Do read the detailed usage examples in the description)
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
private String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
You can try Joda-time's DateTime.
精彩评论