Tag Archives: java

PHP Arrays in JAVA

Going from PHP to Java is a step backwards when dealing with arrays (imo).  One of the more beneficial things that you can do with a PHP array is define keys.  For example I can do something like:

$myList = array("oranges"=>15, "apples"=>25);

Then I can access how many oranges I have by going echo $myList[“oranges”];

To do something like this in JAVA is a bit different than just defining a normal array. In JAVA you need to use a Map:

HashMap myList = new HashMap();
myList.put("oranges", 15);
myList.put("apples", 25);

If you need to store two strings, then change the object type (I.e. HashMap).
To get the value, simply use the get method.


How to use Unix Timestamp with Java

You can use a unix timestamp in java wit the following code:

import java.util.Date;
String TimeStamp = "1324716371";
Date time=new Date((long)TimeStamp*1000);

Now, timestamp doesn’t have to be a string, however in many of your projects you may have the value in that format. In this case, we convert it to a long (integer is too small), and then multiply it by 1000 since Java uses milliseconds.