3

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.

Jens Bergvall
  • 1,617
  • 2
  • 24
  • 54
  • This question can be answered with just a cursory look in the Java documentation. Why do you expect us to do that for you? – Sven Jul 27 '11 at 10:41
  • @sven I was asking for pointers, not for a complete answer. I did not expect a total answer. – Jens Bergvall Jul 27 '11 at 10:44

3 Answers3

2

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)

Susam Pal
  • 32,765
  • 12
  • 81
  • 103
  • Thankyou! Highly appreciated, i was looking for something like this. – Jens Bergvall Jul 27 '11 at 10:45
  • Hi, I am myself learning Java and I am finding the "Head First Java" book (http://www.amazon.com/Head-First-Java-Kathy-Sierra/dp/0596009208/ref=sr_1_1?ie=UTF8&qid=1311763851&sr=8-1) very helpful and instructive. You might want to check it out. – EKI Jul 27 '11 at 10:52
0
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);
}
Destructor
  • 1,641
  • 1
  • 13
  • 13
0

You can try Joda-time's DateTime.

Emil
  • 13,577
  • 18
  • 69
  • 108