Skip to content

Instantly share code, notes, and snippets.

@dsaiztc
dsaiztc / Haversine distance.java
Last active January 21, 2019 09:11
Calculate distance between two GPS points.
/**
* Calculate distance between two gps points based in Haversine formula: http://en.wikipedia.org/wiki/Haversine_formula
*
* @param latitude1 GPS point 1 Latitude
* @param longitude1 GPS point 1 Longitude
* @param latitude2 GPS point 2 Latitude
* @param longitude2 GPS point 1 Longitude
* @return Distance in kilometers (km)
*/
static double haversineDistance(float latitude1, float longitude1, float latitude2, float longitude2)
@dsaiztc
dsaiztc / From binary to Java byte.java
Created June 7, 2014 10:25
Convert an unsigned integer to signed (keeping binary value).
/**
* Convert an unsigned integer to signed (keeping binary value)
*
* @param binary Integer whose binary value need to convert
* @return A byte with same binary than income integer
*/
private static byte fromBinaryToByte(int binary)
{
byte result = -1;
@dsaiztc
dsaiztc / Convert NMEA to grades.java
Created June 7, 2014 10:27
Converts NMEA GPS format to grades.
/**
* Converts a latitude/longitude in NMEA format to grades (4322.0399N to 43.36715)
* @param lat_or_long latitude/longitude in NMEA format
* @return GPS latitude/longitude in grades
*/
public static float convertLatLon(String lat_or_long)
{
int number_of_decimals = 4; // Número de decimales de los grados
String minute = lat_or_long.substring(lat_or_long.length() - (4 + number_of_decimals), lat_or_long.length() - 1);
@dsaiztc
dsaiztc / Date format (local).java
Created June 7, 2014 10:31
Format Date in local format.
final static DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
DATE_FORMAT.format(new Date());
@dsaiztc
dsaiztc / Object to byte[].java
Created June 7, 2014 10:31
Convert object to byte[].
/**
* Converts an object to a byte array
*
* @param object Object to convert (must implement Serializable)
* @return The object in byte[]
*/
public static byte[] objectToByte(Object object)
{
byte[] result = null;
@dsaiztc
dsaiztc / byte[] to object.java
Created June 7, 2014 10:32
Converts byte[] to object.
/**
* Converts an object in byte[] to Object
*
* @param object Object in byte[]
* @return The original Object
*/
public static Object byteToObject(byte[] object)
{
Object result = null;
@dsaiztc
dsaiztc / Convert time slot.java
Created June 7, 2014 10:37
Convert time slot to "00:00:00" format.
String convertTime(long start_time, long end_time)
{
String result = "00:00:00";
if (end_time > start_time)
{
long time = end_time - start_time;
long time_hour = TimeUnit.MILLISECONDS.toHours(time);
time -= TimeUnit.HOURS.toMillis(time_hour);
@dsaiztc
dsaiztc / Schedule task.java
Created June 7, 2014 10:41
Shedule periodic tasks.
ScheduledExecutorService mScheduledExecutorService = Executors.newScheduledThreadPool(1);;
Future<?> mFuture = mScheduledExecutorService.scheduleAtFixedRate(new Runnable()
{
public void run()
{
// Task to do periodically
}
}, 0L, updateTime, TimeUnit.SECONDS);
@dsaiztc
dsaiztc / AsyncTask.java
Created June 7, 2014 10:45
AsyncTask in Android
private class MyTask extends AsyncTask<Void, String, Boolean>
{
private ProgessDialog mProgressDialog;
public MyTask()
{
mProgressDialog = new ProgressDialog(mActivity);
mProgressDialog.setTitle("ProgressDialog title");
mProgressDialog.setMessage("ProgressDialog message...");
}
@dsaiztc
dsaiztc / Write to file.java
Created June 9, 2014 08:32
Write to file in Java/Android
// Save to storage/folder/
File fileDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/folder/");
fileDir.mkdirs();
String name = "file.txt";
File mFile = new File(fileDir, name);
BufferedWriter writer = null;
try
{