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.

2
  • This question can be answered with just a cursory look in the Java documentation. Why do you expect us to do that for you? Commented Jul 27, 2011 at 10:41
  • @sven I was asking for pointers, not for a complete answer. I did not expect a total answer. Commented Jul 27, 2011 at 10:44

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Thankyou! Highly appreciated, i was looking for something like this.
Hi, I am myself learning Java and I am finding the "Head First Java" book (amazon.com/Head-First-Java-Kathy-Sierra/dp/0596009208/…) very helpful and instructive. You might want to check it out.
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); } 

Comments

0

You can try Joda-time's DateTime.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.