Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. package zad2;
  2.  
  3.  
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.time.LocalDate;
  7. import java.time.LocalDateTime;
  8. import java.time.ZoneId;
  9. import java.util.Calendar;
  10. import java.util.Date;
  11. import java.util.GregorianCalendar;
  12.  
  13. public class timeClass {
  14. @SuppressWarnings("deprecation")
  15. public static void main(String[] args) throws ParseException {
  16.  
  17. calcDaysWW2();
  18. day68(2016);
  19. calcDaysFromYourDate();
  20. dateCalc("10:00","17:00");
  21.  
  22. }
  23.  
  24. private static void day68(int year) {
  25. SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd");
  26. Calendar calendar = new GregorianCalendar(year,0,01);
  27. //System.out.println("Date : " + sdf.format(calendar.getTime()));
  28. String months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  29. "Oct", "Nov", "Dec"};
  30. String daysofWeek[] = {"", "Mon","Tue","Wedn","Thursday","Friday","Saturday","Sunday"}; // 0 is empty
  31.  
  32. calendar.add(Calendar.DAY_OF_MONTH, 68);
  33. //System.out.println("Date : " + sdf.format(calendar.getTime()));
  34. int month = calendar.get(Calendar.MONTH); // Jan = 0, dec = 11
  35. int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
  36. //System.out.println("Month " + months[month]);
  37. System.out.println("68 day of 206 is " + daysofWeek[dayOfWeek]);
  38.  
  39.  
  40.  
  41.  
  42. }
  43.  
  44. private static void calcDaysWW2() {
  45. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  46.  
  47.  
  48. Date startWW2 = new Date(1900,9,1);
  49. Date endWW2 = new Date(1945,9,1);
  50.  
  51. try{
  52. startWW2 = format.parse ( "1939-12-31" );
  53. } catch (ParseException e){
  54. e.printStackTrace();
  55. }
  56.  
  57. try {
  58. endWW2 = format.parse ( "1945-12-31" );
  59. } catch (ParseException e) {
  60. e.printStackTrace();
  61. }
  62.  
  63. long days1 = endWW2.getTime() / (24 * 60 * 60 * 1000) - startWW2.getTime() / (24 * 60 * 60 * 1000);
  64. System.out.println("WW2 time : " + days1);
  65.  
  66. }
  67.  
  68. private static void calcDaysFromYourDate() {
  69. LocalDate localDate ;
  70. Calendar calendar = new GregorianCalendar(1997,9,6);
  71. int currentYear = Calendar.getInstance().get(Calendar.YEAR);
  72.  
  73. calendar.add(Calendar.YEAR, 1);
  74. //System.out.println(calendar.get(Calendar.YEAR));
  75. int count = 0; //count of 29 fab in my life
  76.  
  77. while(calendar.get(Calendar.YEAR)<currentYear)
  78. {
  79. localDate = LocalDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId()).toLocalDate();
  80. if(localDate.isLeapYear()){
  81. count++;
  82. }
  83. calendar.add(Calendar.YEAR, 1);
  84. }
  85.  
  86. System.out.println("count of 29 Fab :" +count);
  87.  
  88.  
  89. }
  90.  
  91. private static void dateCalc(String string, String string2) throws ParseException {
  92.  
  93.  
  94.  
  95.  
  96. //SimpleDateFormat format = new SimpleDateFormat("hh-mm");
  97. //Date startWW2 = format.parse ( "10-20" );
  98. //now.setTime(startWW2);
  99.  
  100. //now.MINUTE
  101. //startWW2 = now.getTime();
  102. //System.out.println(startWW2);
  103. Calendar destinyTime = Calendar.getInstance();
  104. Calendar now = Calendar.getInstance();
  105.  
  106. SimpleDateFormat format = new SimpleDateFormat("hh:mm");
  107. Date newTime = format.parse ( string );
  108. now.setTime(newTime);
  109. Date destiny = format.parse ( string2 );
  110. destinyTime.setTime(destiny);
  111. String computeString;
  112. int i = 0 ;
  113.  
  114.  
  115. while(true)
  116. {
  117. if((now.get(Calendar.MINUTE) == destinyTime.get(Calendar.MINUTE) )&& (now.get(Calendar.HOUR) == destinyTime.get(Calendar.HOUR))) {
  118.  
  119.  
  120. break;
  121. }
  122. //System.out.println(now.get(Calendar.MINUTE)+destinyTime.get(Calendar.MINUTE));
  123. //System.out.println(now.get(Calendar.HOUR) + destinyTime.get(Calendar.HOUR));
  124. computeString = Integer.toString(now.get(Calendar.MINUTE)) + Integer.toString(now.get(Calendar.HOUR));
  125. // System.out.println(computeString);
  126. if(sumOfDigits(computeString)==15){
  127. i++;
  128.  
  129. }
  130. now.add(Calendar.MINUTE,1);
  131. }
  132. System.out.println("Count of 15 : " + i);
  133.  
  134.  
  135.  
  136. }
  137.  
  138.  
  139.  
  140. public static int sumOfDigits(String s1) {
  141. int sum = 0;
  142. for (int i = 0; i < s1.length(); i++) {
  143. char a = s1.charAt(i);
  144. if (Character.isDigit(a)) {
  145. int b = Integer.parseInt(String.valueOf(a));
  146. sum = sum + b;
  147. }
  148. }
  149. return sum;
  150. }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement