2013年5月24日金曜日

How to parse .NET DateTime Ticks value in Java

I had to consume an XML data that contains .NET DateTime Ticks (long type) value. This should do it.


import java.util.*;

public class DotNetTicksUtil 
{
  
    private static final long TICKS_AT_EPOCH = 621355968000000000L;
    private static final long TICKS_PER_MILLISECOND = 10000;

    /**
     * Given a .NET ticks value, we convert it to Java's Calendar object.
     * Conversion may not be perfectly precise, so do expect some margin of errors.
     */
    public static Calendar toCalender(long ticks) 
    {
        //long ticks = 634200192000000000L;

        Date date = new Date((ticks - TICKS_AT_EPOCH) / TICKS_PER_MILLISECOND);
        System.out.println(date);

        TimeZone utc = TimeZone.getTimeZone("UTC");
        Calendar calendar = Calendar.getInstance(utc);
        calendar.setTime(date);
        return calendar;
    }
}

2013年5月22日水曜日

Note to myself: Creating a self-installing .NET Windows Service

(This is not about Java. Its about .NET)

It took me while to get it working, so I am writing a note to myself for future reference.

  • In Visual Studio .NET IDE, do "Add -> New Item..."
  • Select "Installer Class"

Your new installer class is added to your project. Next, we want to add ServiceProcessInstaller and ServiceInstaler from the Toolbox but you won't see them in the Toolbox by default.

To make these two components to show up:

  • Right-mouse click on Toolbox and select "Choose items..."
  • Look for these components and select them.

Both SeviceProcessInstaller and ServiceInstaller components will show up in the Toolbox, so drag them to the Installer's designer.

Select each item on the designer and F4 to show its properties and configure them (Google to learn details about  what to set)

That should do.

Earlier, I tried to create an Installer class manually without using IDE designer and I ran into too many weird issues. Then, the designer approach worked in two minutes.