Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zad2;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- import java.time.ZoneId;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- public class timeClass {
- @SuppressWarnings("deprecation")
- public static void main(String[] args) throws ParseException {
- calcDaysWW2();
- day68(2016);
- calcDaysFromYourDate();
- dateCalc("10:00","17:00");
- }
- private static void day68(int year) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd");
- Calendar calendar = new GregorianCalendar(year,0,01);
- //System.out.println("Date : " + sdf.format(calendar.getTime()));
- String months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
- "Oct", "Nov", "Dec"};
- String daysofWeek[] = {"", "Mon","Tue","Wedn","Thursday","Friday","Saturday","Sunday"}; // 0 is empty
- calendar.add(Calendar.DAY_OF_MONTH, 68);
- //System.out.println("Date : " + sdf.format(calendar.getTime()));
- int month = calendar.get(Calendar.MONTH); // Jan = 0, dec = 11
- int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
- //System.out.println("Month " + months[month]);
- System.out.println("68 day of 206 is " + daysofWeek[dayOfWeek]);
- }
- private static void calcDaysWW2() {
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Date startWW2 = new Date(1900,9,1);
- Date endWW2 = new Date(1945,9,1);
- try{
- startWW2 = format.parse ( "1939-12-31" );
- } catch (ParseException e){
- e.printStackTrace();
- }
- try {
- endWW2 = format.parse ( "1945-12-31" );
- } catch (ParseException e) {
- e.printStackTrace();
- }
- long days1 = endWW2.getTime() / (24 * 60 * 60 * 1000) - startWW2.getTime() / (24 * 60 * 60 * 1000);
- System.out.println("WW2 time : " + days1);
- }
- private static void calcDaysFromYourDate() {
- LocalDate localDate ;
- Calendar calendar = new GregorianCalendar(1997,9,6);
- int currentYear = Calendar.getInstance().get(Calendar.YEAR);
- calendar.add(Calendar.YEAR, 1);
- //System.out.println(calendar.get(Calendar.YEAR));
- int count = 0; //count of 29 fab in my life
- while(calendar.get(Calendar.YEAR)<currentYear)
- {
- localDate = LocalDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId()).toLocalDate();
- if(localDate.isLeapYear()){
- count++;
- }
- calendar.add(Calendar.YEAR, 1);
- }
- System.out.println("count of 29 Fab :" +count);
- }
- private static void dateCalc(String string, String string2) throws ParseException {
- //SimpleDateFormat format = new SimpleDateFormat("hh-mm");
- //Date startWW2 = format.parse ( "10-20" );
- //now.setTime(startWW2);
- //now.MINUTE
- //startWW2 = now.getTime();
- //System.out.println(startWW2);
- Calendar destinyTime = Calendar.getInstance();
- Calendar now = Calendar.getInstance();
- SimpleDateFormat format = new SimpleDateFormat("hh:mm");
- Date newTime = format.parse ( string );
- now.setTime(newTime);
- Date destiny = format.parse ( string2 );
- destinyTime.setTime(destiny);
- String computeString;
- int i = 0 ;
- while(true)
- {
- if((now.get(Calendar.MINUTE) == destinyTime.get(Calendar.MINUTE) )&& (now.get(Calendar.HOUR) == destinyTime.get(Calendar.HOUR))) {
- break;
- }
- //System.out.println(now.get(Calendar.MINUTE)+destinyTime.get(Calendar.MINUTE));
- //System.out.println(now.get(Calendar.HOUR) + destinyTime.get(Calendar.HOUR));
- computeString = Integer.toString(now.get(Calendar.MINUTE)) + Integer.toString(now.get(Calendar.HOUR));
- // System.out.println(computeString);
- if(sumOfDigits(computeString)==15){
- i++;
- }
- now.add(Calendar.MINUTE,1);
- }
- System.out.println("Count of 15 : " + i);
- }
- public static int sumOfDigits(String s1) {
- int sum = 0;
- for (int i = 0; i < s1.length(); i++) {
- char a = s1.charAt(i);
- if (Character.isDigit(a)) {
- int b = Integer.parseInt(String.valueOf(a));
- sum = sum + b;
- }
- }
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement