Monday, 21 December 2015

Create unique ID : use timestamp for it in Java

Whenever we require a unique ID for different purposes. We try with various approach.
One of good approach is to use timestamp with specific format. Please find the code for the same.

getTrackignID function will give you unique ID for the use.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GetUniqueID {


         String defaultFormat="yyMMddHHmmss";
         String trackingID=null;

         private final long startTime=System.nanoTime();

private static final long deltaNanos = System.currentTimeMillis() * 1000000L - System.nanoTime();

private DateFormat dateFormat;

public DateFormat getDateFormat() {
return dateFormat;
}

public void setDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat;
}

public long getStartTime()
{
return ((this.startTime + deltaNanos) / 1000000L);
}

         public String getTrackignID(){
setDateFormat(new SimpleDateFormat(defaultFormat));
String trackingID=null;
trackingID=getTimestamp();
System.out.println("Generated tracking ID :"+trackingID);
return trackingID;

}

private String getTimestamp() {
return this.dateFormat.format(new Date(getStartTime()));
}
}

output will in format below :

151221123634 as per the format "yyMMddHHmmss"

Get XMLGregorianCalendar timestamp in Java


Get XMLGregorianCalendar timestamp in format :

2016-01-01T11:58:58-05:00

import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public static XMLGregorianCalendar getTimeStamp() throws DatatypeConfigurationException{
Date trailDate=new Date();
XMLGregorianCalendar timestampdate=null;
    GregorianCalendar calendar1 = new GregorianCalendar();
    calendar1.setTime(trailDate);
    try
    {
    timestampdate=DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar1);
    System.out.println("timestampdate :"+timestampdate);
    return timestampdate;
    }
   catch (DatatypeConfigurationException ex)
    {
  System.out.println("Exception in getting xmlGreg Date, setting bydefault a value for Calender");
  return  DatatypeFactory.newInstance().newXMLGregorianCalendar("2016-01-01T11:58:58-05:00");
    }
}


Get shortened stacktrace after exception in Java


Sometimes, we require only set of lines from the stacktrace, so a generic method to get the required number of line from stacktrace.

import java.io.PrintWriter;
import java.io.StringWriter;

public static void main(String[] args) {
       try {
       throw new Exception("Malformed Exception");
   } catch (Exception e) {
       System.err.println(shortenedStackTrace(e, 1));
   }
}

public static String shortenedStackTrace(Exception e, int maxLines) {
   StringWriter writer = new StringWriter();
   e.printStackTrace(new PrintWriter(writer));
   String[] lines = writer.toString().split("\n");
   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < Math.min(lines.length, maxLines); i++) {
       sb.append(lines[i]).append("\n");
   }
   return sb.toString();
}