Math والتاريخ

في هذا الدرس، سنتعلم كيفية استخدام فئة Math للعمليات الحسابية وفئة التاريخ والوقت.

فئة Math

Math هي فئة تحتوي على دوال حسابية ثابتة.

الثوابت

JAVA
double pi = Math.PI;   // 3.141592653589793
double e = Math.E;     // 2.718281828459045

الدوال الأساسية

الدالة الوصف مثال
abs(x) القيمة المطلقة Math.abs(-5)5
max(a, b) الأكبر Math.max(5, 3)5
min(a, b) الأصغر Math.min(5, 3)3
pow(a, b) الأس Math.pow(2, 3)8.0
sqrt(x) الجذر التربيعي Math.sqrt(16)4.0
round(x) التقريب Math.round(3.7)4
ceil(x) التقريب للأعلى Math.ceil(3.2)4.0
floor(x) التقريب للأسفل Math.floor(3.8)3.0
random() رقم عشوائي Math.random()0.0 إلى 1.0

أمثلة

JAVA
// القيمة المطلقة
System.out.println(Math.abs(-10));  // 10

// الأس
System.out.println(Math.pow(2, 10));  // 1024.0

// الجذر التربيعي
System.out.println(Math.sqrt(144));  // 12.0

// التقريب
System.out.println(Math.round(3.5));  // 4
System.out.println(Math.ceil(3.1));   // 4.0
System.out.println(Math.floor(3.9));  // 3.0

أرقام عشوائية

JAVA
// رقم عشوائي بين 0 و 1
double random = Math.random();

// رقم عشوائي بين 0 و 99
int randomInt = (int) (Math.random() * 100);

// رقم عشوائي بين min و max
int randomInRange = (int) (Math.random() * (max - min + 1)) + min;

التاريخ والوقت (Java 8+)

LocalDate — التاريخ

JAVA
import java.time.LocalDate;

// تاريخ اليوم
LocalDate today = LocalDate.now();
System.out.println(today);  // 2026-06-25

// تاريخ محدد
LocalDate birthday = LocalDate.of(2000, 1, 15);
System.out.println(birthday);  // 2000-01-15

// الوصول إلى الأجزاء
int year = today.getYear();       // 2026
int month = today.getMonthValue(); // 6
int day = today.getDayOfMonth();   // 25

LocalTime — الوقت

JAVA
import java.time.LocalTime;

// الوقت الحالي
LocalTime now = LocalTime.now();
System.out.println(now);  // 14:30:45.123

// وقت محدد
LocalTime time = LocalTime.of(14, 30, 0);
System.out.println(time);  // 14:30:00

// الوصول إلى الأجزاء
int hour = now.getHour();     // 14
int minute = now.getMinute(); // 30
int second = now.getSecond(); // 45

LocalDateTime — التاريخ والوقت

JAVA
import java.time.LocalDateTime;

// التاريخ والوقت الحالي
LocalDateTime now = LocalDateTime.now();
System.out.println(now);  // 2026-06-25T14:30:45.123

// تاريخ ووقت محدد
LocalDateTime specific = LocalDateTime.of(2026, 12, 25, 10, 0);
System.out.println(specific);  // 2026-12-25T10:00

تنسيق التاريخ

JAVA
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

LocalDateTime now = LocalDateTime.now();

// تنسيق مخصص
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatted = now.format(formatter);
System.out.println(formatted);  // 2026-06-25 14:30:45

// تنسيق آخر
DateTimeFormatter arabic = DateTimeFormatter.ofPattern("dd/MM/yyyy");
System.out.println(now.format(arabic));  // 25/06/2026

تحليل النص إلى تاريخ

JAVA
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

String dateStr = "25/06/2026";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date = LocalDate.parse(dateStr, formatter);
System.out.println(date);  // 2026-06-25

العمليات على التاريخ

JAVA
import java.time.LocalDate;

LocalDate today = LocalDate.now();

// إضافة
LocalDate nextWeek = today.plusDays(7);
LocalDate nextMonth = today.plusMonths(1);
LocalDate nextYear = today.plusYears(1);

// طرح
LocalDate lastWeek = today.minusDays(7);
LocalDate lastMonth = today.minusMonths(1);

// المقارنة
boolean isBefore = today.isBefore(nextWeek);  // true
boolean isAfter = today.isAfter(lastWeek);    // true
boolean isSame = today.isEqual(LocalDate.now());  // true

Duration — المدة

JAVA
import java.time.Duration;
import java.time.LocalTime;

LocalTime start = LocalTime.of(9, 0);
LocalTime end = LocalTime.of(17, 30);

Duration duration = Duration.between(start, end);
System.out.println(duration.toHours());    // 8
System.out.println(duration.toMinutes());  // 510

❓ أسئلة شائعة

س ما الفرق بين Math.random() و Random؟
ج Math.random() أبسط. Random أكثر مرونة ويوفر أنواع بيانات مختلفة.
س لماذا نستخدم LocalDate بدلاً من Date؟
ج LocalDate حديثة (Java 8+) وأسهل في الاستخدام وآمنة للخيوط. Date قديمة ومعقدة.
س كيف أحدد المنطقة الزمنية؟
ج استخدم ZonedDateTime.now(ZoneId.of("Asia/Riyadh")).

📖 ملخص

📝 تمارين

  1. حساب المسافة: اكتب دالة تحسب المسافة بين نقطتين (x1، y1) و (x2، y2)
  2. العمر: اكتب دالة تحسب العمر بالسنوات والأشهر والأيام
  3. مؤقت: اكتب برنامج يعد تنازليًا من 10 إلى 0

الدرس التالي

في الدرس التالي، سنتعلم ممارسة النصوص — تطبيقات عملية.

100%