Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class Main {
- public static String convertDateToBinary(String date) {
- String binary = "";
- String[] split = date.split("-");
- int[] data = new int[split.length];
- for (int i = 0; i < split.length; i++) {
- data[i] = Integer.parseInt(split[i]);
- }
- for (int i = 0; i < data.length; i++) {
- String temp = "";
- while (data[i] != 0) {
- //get the reversed binary of the fisrt num (date)
- temp += data[i] % 2;
- data[i] /= 2;
- }
- //reverse the temp so it is a binary
- for (int j = temp.length()-1; j>=0 ;j--) {
- binary += temp.charAt(j);
- }
- //if the num is not the last, space them with -
- if(i!=data.length-1) binary += "-";
- }
- return binary;
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String allowed = sc.nextLine();
- System.out.println(convertDateToBinary(allowed));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement