iT邦幫忙

0

java 轉python 一問

  • 分享至 

  • xImage

請問一下以下這段code 裡面的int estimate = (int)getEstDiff(calUnit, d1, d2);
這段之後的在python 是怎樣表達, 謝謝

public static int getDateDiff(String Field, Date d1, Date d2) 
	{
		if (d1 == null | d2 == null) return 0;
		
		// swap if d1 later than d2
		boolean neg = false;
		if( d1.after(d2) ) {
			 Date temp = d1;
			 d1 = d2;
			 d2 = temp;
			 neg = true;
		  }
		  
		Field = Field.toUpperCase();
		int calUnit;
		if (Field.compareTo("Y") == 0)
			calUnit = Calendar.YEAR;
		else if (Field.compareTo("M") == 0)
			calUnit = Calendar.MONTH;
		else if (Field.compareTo("W") == 0)
			calUnit = Calendar.WEEK_OF_YEAR;
		else if (Field.compareTo("D") == 0)
			calUnit = Calendar.DAY_OF_MONTH;
		else if (Field.compareTo("H") == 0)
			calUnit = Calendar.HOUR;
		else if (Field.compareTo("MI") == 0)
			calUnit = Calendar.MINUTE;
		else 
			calUnit = Calendar.SECOND;
					
		// estimate the diff.  d1 is now guaranteed <= d2
		int estimate = (int)getEstDiff(calUnit, d1, d2);

		// convert the Dates to GregorianCalendars
		GregorianCalendar c1 = new GregorianCalendar();
		//c1.set(d1.getYear(), d1.getMonth(), d1.getDate(), d1.getHours(), d1.getMinutes(), d1.getSeconds());
		c1.setTime(d1);
		GregorianCalendar c2 = new GregorianCalendar();
		//c2.set(d2.getYear(), d2.getMonth(), d2.getDate(), d2.getHours(), d2.getMinutes(), d2.getSeconds());
		c2.setTime(d2);

		// add 2 units less than the estimate to 1st date,
		//  then serially add units till we exceed 2nd date
		c1.add( calUnit, (int)estimate - 2 );
		for( int i=estimate-1; ; i++ ) 
		{
			c1.add(calUnit, 1);
			if(c1.after(c2))
				return neg ? 1-i : i-1;
		}
   }
froce iT邦大師 1 級 ‧ 2022-04-01 13:48:09 檢舉
estimate = int(getEstDiff(calUnit, d1, d2))

你 getEstDiff 是int吧?還是float? 不然幹嘛要強制轉型
diu7me iT邦新手 4 級 ‧ 2022-04-01 14:58:46 檢舉
是上一手寫的, 我只是把它換成python
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答