Sunday, April 24, 2016

No Time Zone in java.util.Date



We can change the time zone of the local machine by changing the default timezone of the JVM like below.

TimeZone.setDefault(TimeZone.getTimeZone("NEW_TIME_ZONE");

A java.util.Date has no time zone†. It represents UTC/GMT (no time zone offset). Very confusing because its toString method applies the JVM's default time zone when generating a String representation.

Avoid j.u.Date

For this and many other reasons, you should avoid using the built-in java.util.Date & .Calendar & java.text.SimpleDateFormat. They are notoriously troublesome.

Instead use the java.time package bundled with Java 8. These new classes are inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. For Java 6 & 7, use the back-port project, ThreeTen-Backport. For Android, the adaptation of that back-port, ThreeTenABP. See Oracle Tutorial.
java.time

The java.time classes can represent a moment on the timeline in three ways:

  • UTC (Instant)
  • With an offset (OffsetDateTime with ZoneOffset)
  • With a time zone (ZonedDateTime with ZoneId)


Instant

In java.time, the basic building block is Instant, a moment on the time line in UTC. Use Instantobjects for much of your business logic.Instant instant = Instant.now();

OffsetDateTime

Apply an offset-from-UTC to adjust into some locality’s wall-clock time.

Apply a ZoneOffset to get an OffsetDateTime.ZoneOffset zoneOffset = ZoneOffset.of( "-04:00" ); OffsetDateTime odt = OffsetDateTime.ofInstant( instant , zoneOffset );

ZonedDateTime

Better is to apply a time zone, an offset plus the rules for handling anomalies such as Daylight Saving Time (DST).

Apply a ZoneId to an Instant to get a ZonedDateTime. Always specify a proper time zone name. Never use 3-4 abbreviations such as EST or IST that are neither unique nor standardized.ZoneId zoneId = ZoneId.of( "America/Montreal" ); ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );

Formatted Strings

Call the toString method on any of these three classes to generate a String representing the date-time value in standard ISO 8601 format. The ZonedDateTime class extends standard format by appending the name of the time zone in brackets.

String outputInstant = instant.toString(); // Ex: 2011-12-03T10:15:30Z 
String outputOdt = odt.toString(); // Ex: 2007-12-03T10:15:30+01:00 
String outputZdt = zdt.toString(); // Ex: 2007-12-03T10:15:30+01:00[Europe/Paris]


For other formats use the DateTimeFormatter class. Generally best to let that class generate localized formats using the user’s expected human language and cultural norms. Or you can specify a particular format.
Joda-Time

While Joda-Time is still actively maintained, its makers have told us to migrate to java.time as soon as is convenient. I leave this section intact as a reference, but I suggest using the java.time section above instead.

In Joda-Time, a date-time object (DateTime) truly does know its assigned time zone. That means an offset from UTC and the rules and history of that time zone’s Daylight Saving Time (DST) and other such anomalies.

String input = "2014-01-02T03:04:05"; 
DateTimeZone timeZone = DateTimeZone.forID( "Asia/Kolkata" ); 
DateTime dateTimeIndia = new DateTime( input, timeZone ); 
DateTime dateTimeUtcGmt = dateTimeIndia.withZone( DateTimeZone.UTC );


Call the toString method to generate a String in ISO 8601 format.String output = dateTimeIndia.toString();


Joda-Time also offers rich capabilities for generating all kinds of other String formats.

If required, you can convert from Joda-Time DateTime to a java.util.Date.Java.util.Date date = dateTimeIndia.toDate();


Search StackOverflow for "joda date" to find many more examples, some quite detailed.

Wednesday, February 17, 2016

Custom json parser to convert an array within an array to java.util.List of type String

package com.indrajith;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {



        List<String> a=
        split("[\n" +
                "\t{\n" +
                "\t\t\"id\": \"0001\",\n" +
                "\t\t\"type\": \"donut\",\n" +
                "\t\t\"name\": \"Cake\",\n" +
                "\t\t\"ppu\": 0.55,\n" +
                "\t\t\"batters\":\n" +
                "\t\t\t{\n" +
                "\t\t\t\t\"batter\":\n" +
                "\t\t\t\t\t[\n" +
                "\t\t\t\t\t\t{ \"id\": \"1001\", \"type\": \"Regular\" },\n" +
                "\t\t\t\t\t\t{ \"id\": \"1002\", \"type\": \"Chocolate\" },\n" +
                "\t\t\t\t\t\t{ \"id\": \"1003\", \"type\": \"Blueberry\" },\n" +
                "\t\t\t\t\t\t{ \"id\": \"1004\", \"type\": \"Devil's Food\" }\n" +
                "\t\t\t\t\t]\n" +
                "\t\t\t},\n" +
                "\t\t\"topping\":\n" +
                "\t\t\t[\n" +
                "\t\t\t\t{ \"id\": \"5001\", \"type\": \"None\" },\n" +
                "\t\t\t\t{ \"id\": \"5002\", \"type\": \"Glazed\" },\n" +
                "\t\t\t\t{ \"id\": \"5005\", \"type\": \"Sugar\" },\n" +
                "\t\t\t\t{ \"id\": \"5007\", \"type\": \"Powdered Sugar\" },\n" +
                "\t\t\t\t{ \"id\": \"5006\", \"type\": \"Chocolate with Sprinkles\" },\n" +
                "\t\t\t\t{ \"id\": \"5003\", \"type\": \"Chocolate\" },\n" +
                "\t\t\t\t{ \"id\": \"5004\", \"type\": \"Maple\" }\n" +
                "\t\t\t]\n" +
                "\t},\n" +
                "\t{\n" +
                "\t\t\"id\": \"0002\",\n" +
                "\t\t\"type\": \"donut\",\n" +
                "\t\t\"name\": \"Raised\",\n" +
                "\t\t\"ppu\": 0.55,\n" +
                "\t\t\"batters\":\n" +
                "\t\t\t{\n" +
                "\t\t\t\t\"batter\":\n" +
                "\t\t\t\t\t[\n" +
                "\t\t\t\t\t\t{ \"id\": \"1001\", \"type\": \"Regular\" }\n" +
                "\t\t\t\t\t]\n" +
                "\t\t\t},\n" +
                "\t\t\"topping\":\n" +
                "\t\t\t[\n" +
                "\t\t\t\t{ \"id\": \"5001\", \"type\": \"None\" },\n" +
                "\t\t\t\t{ \"id\": \"5002\", \"type\": \"Glazed\" },\n" +
                "\t\t\t\t{ \"id\": \"5005\", \"type\": \"Sugar\" },\n" +
                "\t\t\t\t{ \"id\": \"5003\", \"type\": \"Chocolate\" },\n" +
                "\t\t\t\t{ \"id\": \"5004\", \"type\": \"Maple\" }\n" +
                "\t\t\t]\n" +
                "\t},\n" +
                "\t{\n" +
                "\t\t\"id\": \"0003\",\n" +
                "\t\t\"type\": \"donut\",\n" +
                "\t\t\"name\": \"Old Fashioned\",\n" +
                "\t\t\"ppu\": 0.55,\n" +
                "\t\t\"batters\":\n" +
                "\t\t\t{\n" +
                "\t\t\t\t\"batter\":\n" +
                "\t\t\t\t\t[\n" +
                "\t\t\t\t\t\t{ \"id\": \"1001\", \"type\": \"Regular\" },\n" +
                "\t\t\t\t\t\t{ \"id\": \"1002\", \"type\": \"Chocolate\" }\n" +
                "\t\t\t\t\t]\n" +
                "\t\t\t},\n" +
                "\t\t\"topping\":\n" +
                "\t\t\t[\n" +
                "\t\t\t\t{ \"id\": \"5001\", \"type\": \"None\" },\n" +
                "\t\t\t\t{ \"id\": \"5002\", \"type\": \"Glazed\" },\n" +
                "\t\t\t\t{ \"id\": \"5003\", \"type\": \"Chocolate\" },\n" +
                "\t\t\t\t{ \"id\": \"5004\", \"type\": \"Maple\" }\n" +
                "\t\t\t]\n" +
                "\t}\n" +
                "]","topping");

    }


    private static List<String> split(String json,String field) {

        char startChar = '{';
        char endChar = '}';


        List<String> returnList = new ArrayList<>();

        if (field != null && !field.isEmpty()) {

            json = json.split(field)[1];

        }

        for (int i = 0; i < json.length(); i++) {

            if (json.charAt(i) == startChar) {
                int count = 0;

                for (int j = 0; j < json.length(); j++) {

                    if (json.charAt(j) == startChar) {
                        count++;
                    }

                    if (json.charAt(j) == startChar) {
                        count--;
                    }

                    if(count==0){
                        returnList.add(json.substring(i,j+1));
                        i=j;
                        break;

                    }


                }


            }
        }

        return returnList;
    }
}

Sunday, February 14, 2016

How to Completely Install Oracle JDK on Linux Mint 17.3 Rosa

I followed the methods given in the below linux mint forum and I was able to successfully install oracle jdk 1.8.0_73.

http://community.linuxmint.com/tutorial/view/1839

Play HEVC video format in VLC player Linux Mint 17.3 Rosa



VLC out of the box does not play HEVC files. To fix this we need to install libde265 via PPA, which is an open source implementation of the h.265 video codec.

Open up the Terminal and run the commands below:

  • sudo apt-add-repository ppa:strukturag/libde265 
  • sudo apt-get update 
  • sudo apt-get install vlc-plugin-libde265 

VLC should now play these media files.

Source: http://www.unixmen.com/fix-vlc-not-support-audio-video-format-hevc/

Fix for wifi signal dropping with Realtek RTL8723BE wifi adapter in Linux mint 17.3 Rosa


http://forums.linuxmint.com/viewtopic.php?t=204172&f=53

In a terminal type the following commands

1. sudo apt-get install linux-headers-generic build-essential git

2. git clone https://github/com/lwfinger/rtlwifi_new

3. cd rtlwifi_new

4. make

5. sudo make install

6. sudo modprobe rtl8723be

rtl8723be was my wireless ..change rtl8723be to your model



If it's still not working...
http://www.dedoimedo.com/computers/ubuntu-trusty-realtek.html

1. sudo gedit /etc/modprobe.d/rtl8723be.conf 

2. add> options rtl8723be fwlps=N ips=N 
 
3. sudo modprobe rtl8723be


check for wifi adapter information:

lspci -vq | grep -i wireless -B 1 -A 5

modinfo rtl8723be

Tuesday, January 12, 2016

Convert json string to an array list in java

import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import com.google.gson.Gson;


String json="{}";

Gson gson = new Gson();

Collection<Document> docCollection= null;

List<Document> documentList=null;

docCollection=gson.fromJson(json,new TypeToken<List<Document>>(){}.getType());

if(docCollection instanceof  List){
documentList=(List<Document>)docCollection;

}else{
documentList=new ArrayList<Document>(docCollection);
}