Category Archives: how to

Java : How to convert seconds to Time?


In this blog, you will learn to convert seconds to time. An hour has 60*60=3600 seconds and a minute has 60 seconds.

Example
In this example helps you to convert total seconds to time as (hour, minute an d second). To show as text String convert these these values to String.

public class ConvertSecondToTime {

public static void main(String[] args) {
 long time = 180500700;
 ConvertSecondToTime ts = new ConvertSecondToTime();
 System.out.println("Inserted Total Seconds :"+time);
 System.out.println(ts.secondsToTimeString(time));
}

public static String secondsToTimeString(long time) {
 int seconds = (int) (time % 60);
 int minutes = (int) ((time / 60) % 60);
 int hours = (int) ((time / 3600) % 24);
 String secondsTxt = (seconds < 10 ? "0" : "") + seconds;
 String minutesTxt = (minutes < 10 ? "0" : "") + minutes;
 String hoursTxt = (hours < 10 ? "0" : "") + hours;
 return new String("Converted Time :"+ hoursTxt + " Hour : " + minutesTxt + " Minute :" + secondsTxt + " Second");
 }
}

Output


Inserted Total Seconds :180500700
Converted Time :03 Hour : 05 Minute :00 Second

Advertisements

Java : Convert Decimal to Binary


In this blog, you will learn to convert decimal number into binary string. The java.lang package provides api’s to convert a decimal number into a binary number.

Example
This program takes a decimal number from user as string and convert it to decimal number. toBinaryString() method takes an integer type value and returns its string representation of integer values which represents the data in binary. The base of binary number is 2.

import java.util.Scanner;

public class ConvertDecimalToBinary {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

System.out.println("Enter the decimal value:");
String hex = scan.nextLine();
// convert String to Int
int i = Integer.parseInt(hex);
// convert integer to binary
String by = Integer.toBinaryString(i);
// print binary String
System.out.println("Binary: " + by);
}
}

Output


Enter the decimal value:
12
Binary: 1100

Enter the decimal value:
200
Binary: 11001000

Java : How to convert GMT, CST, IST and PST timezone?


In this article, you will learn to convert a date to GMT, CST, EST and PST format.

  • GMT : Greenwich Mean Time
  • CST : Central Standard Time= GMT-5
  • EST : Eastern Standard Time = GMT-6
  • PST : Pacific Standard Time=GMT-7

Example
This example helps you in converting a date to  GMT, CST, EST and PST time on the console. The SimpleDateFormat() constructor uses the given pattern and date format symbols. Here we use the date format as gmtFormat for date format in GMT timezone same way can convert for CST, EST and PST. Then we have used getTimeZone() method to get the time of both the zones to be converted.

import java.util.*;
import java.text.*;

public class ConvertTimeZoneFromGMT {

public static void main(String[] args) {
//Current Date and Time
Date date = new Date();

//GMT Format
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
gmtFormat.setTimeZone(gmtTime);
System.out.println("GMT Time: " + gmtFormat.format(date));

//CST Time
DateFormat cstFormat = new SimpleDateFormat();
TimeZone cstTime = TimeZone.getTimeZone("CST");
cstFormat.setTimeZone(cstTime);
System.out.println("CST Time: " + cstFormat.format(date));

//EST Time
DateFormat istFormat = new SimpleDateFormat();
TimeZone estTime = TimeZone.getTimeZone("EST");
estFormat.setTimeZone(estTime);
System.out.println("EST Time: " + estFormat.format(date));

// PST Time
DateFormat pstFormat = new SimpleDateFormat();
TimeZone pstTime = TimeZone.getTimeZone("PST");
pstFormat.setTimeZone(pstTime);
System.out.println("PST Time: " + pstFormat.format(date)); 

}
}

Output


GMT Time: 3/22/19 8:39 AM
CST Time: 3/22/19 3:39 AM
EST Time: 3/22/19 2:09 PM
PST Time: 3/22/19 1:39 AM