Advertisement
BojidarDosev

3280. Convert Date to Binary

May 14th, 2025
798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class Main {
  5.     public static String convertDateToBinary(String date) {
  6.         String binary = "";
  7.         String[] split = date.split("-");
  8.         int[] data = new int[split.length];
  9.  
  10.         for (int i = 0; i < split.length; i++) {
  11.             data[i] = Integer.parseInt(split[i]);
  12.         }
  13.  
  14.         for (int i = 0; i < data.length; i++) {
  15.             String temp = "";
  16.             while (data[i] != 0) {
  17.                 //get the reversed binary of the fisrt num (date)
  18.                 temp += data[i] % 2;
  19.                 data[i] /= 2;
  20.             }
  21.             //reverse the temp so it is a binary
  22.             for (int j = temp.length()-1; j>=0 ;j--) {
  23.                 binary += temp.charAt(j);
  24.             }
  25.             //if the num is not the last, space them with -
  26.             if(i!=data.length-1) binary += "-";
  27.         }
  28.  
  29.         return binary;
  30.     }
  31.  
  32.     public static void main(String[] args) {
  33.         Scanner sc = new Scanner(System.in);
  34.         String allowed = sc.nextLine();
  35.  
  36.         System.out.println(convertDateToBinary(allowed));
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement