How to convert List to Map?

With java-8, you’ll be able to do this in one line using streams, and the Collectors class. Map<String, Item> map = list.stream().collect(Collectors.toMap(Item::getKey, item -> item)); Short demo: import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Test{ public static void main (String [] args){ List<Item> list = IntStream.rangeClosed(1, 4) .mapToObj(Item::new) .collect(Collectors.toList()); //[Item [i=1], Item … Read more

How can I convert a Unix timestamp to DateTime and vice versa?

Here’s what you need: public static DateTime UnixTimeStampToDateTime( double unixTimeStamp ) { // Unix timestamp is seconds past epoch DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); dateTime = dateTime.AddSeconds( unixTimeStamp ).ToLocalTime(); return dateTime; } Or, for Java (which is different because the timestamp is in milliseconds, not seconds): public static … Read more

Java string to date conversion

That’s the hard way, and those java.util.Date setter methods have been deprecated since Java 1.1 (1997). Moreover, the whole java.util.Date class was de-facto deprecated (discommended) since introduction of java.time API in Java 8 (2014). Simply format the date using DateTimeFormatter with a pattern matching the input string (the tutorial is available here). In your specific … Read more