Aks java, Cara menampilkan data berdasarkan hari, minggu, bulan, tahun

selamat siang agan-agan sekolah koding, saya langsung saja ke pertanyaan saya

Bagaimana cara menampilkan data pada database berdasarkan hari, minggu, bulan, tahun ?

mohon bantuannya agan-agan

avatar ariflanitaman1
@ariflanitaman1

21 Kontribusi 0 Poin

Diperbarui 6 tahun yang lalu

2 Jawaban:

"Bagaimana cara menampilkan data pada database berdasarkan hari, minggu, bulan, tahun ?"

Ada 3 Cara yg kamu bisa gunakan: (bisa jg dikombinasikan) 1. SimpleDateFormat 2. Calendar 3. DateFormat

Cara 1 : SimpleDatedFormat


import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTest {
   public static void main(String[] args) {
      Date now = new Date();
      System.out.println("toString(): " + now);  // dow mon dd hh:mm:ss zzz yyyy

      // SimpleDateFormat can be used to control the date/time display format:
      //   E (day of week): 3E or fewer (in text xxx), >3E (in full text)
      //   M (month): M (in number), MM (in number with leading zero)
      //              3M: (in text xxx), >3M: (in full text full)
      //   h (hour): h, hh (with leading zero)
      //   m (minute)
      //   s (second)
      //   a (AM/PM)
      //   H (hour in 0 to 23)
      //   z (time zone)
      SimpleDateFormat dateFormatter = new SimpleDateFormat("E, y-M-d 'at' h:m:s a z");
      System.out.println("Format 1:   " + dateFormatter.format(now));

      dateFormatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
      System.out.println("Format 2:   " + dateFormatter.format(now));

      dateFormatter = new SimpleDateFormat("EEEE, MMMM d, yyyy");
      System.out.println("Format 3:   " + dateFormatter.format(now));
   }
}

Output : SimpleDateFormat


toString(): Sat Sep 25 21:27:01 SGT 2010
Format 1:   Sat, 10-9-25 at 9:27:1 PM SGT
Format 2:   Sat 2010.09.25 at 09:27:01 PM SGT
Format 3:   Saturday, September 25, 2010

Cara 2 : Calendar


// Get the year, month, day, hour, minute, second
import java.util.Calendar;
public class GetYMDHMS {
   public static void main(String[] args) {
      Calendar cal = Calendar.getInstance();
      // You cannot use Date class to extract individual Date fields
      int year = cal.get(Calendar.YEAR);
      int month = cal.get(Calendar.MONTH);      // 0 to 11
      int day = cal.get(Calendar.DAY_OF_MONTH);
      int hour = cal.get(Calendar.HOUR_OF_DAY);
      int minute = cal.get(Calendar.MINUTE);
      int second = cal.get(Calendar.SECOND);

      System.out.printf("Now is %4d/%02d/%02d %02d:%02d:%02d\n",  // Pad with zero
          year, month+1, day, hour, minute, second);
   }
}

Output: Calendar


Now is 2018/04/04 12:12:12

Cara 3 : DateFormat


import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

public class DateFormatExample {
	public static void main(String[] args) {

		Date curDate = new Date();

		System.out.println(curDate.toString());

		String DateToStr = DateFormat.getInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getDateInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getDateTimeInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance(DateFormat.SHORT)
				.format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(
				curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance(DateFormat.LONG).format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
				DateFormat.SHORT).format(curDate);
		System.out.println(DateToStr);

		try {
			Date strToDate = DateFormat.getDateInstance()
					.parse("July 17, 1989");
			System.out.println(strToDate.toString());
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}
}

Output: DateFormat


Sun May 11 23:37:54 EEST 2014
5/11/14 11:37 PM
11:37:54 PM
May 11, 2014
May 11, 2014 11:37:54 PM
11:37 PM
11:37:54 PM
11:37:54 PM EEST
May 11, 2014 11:37 PM
Mon Jul 17 00:00:00 EEST 1989

Source: 1. [link]https://www.ntu.edu.sg/home/ehchua/programming/java/DateTimeCalendar.html [/link] 2. https://examples.javacodegeeks.com/core-java/text/java-simpledateformat-example/ 3. https://examples.javacodegeeks.com/core-java/text/java-dateformat-example/

avatar keccikun
@keccikun

368 Kontribusi 197 Poin

Dipost 6 tahun yang lalu

kalau mau menampilkan data perhari ini saja, atau perminggu ini saja, atau perbulan ini saja, atau pertahun ini saja codingnya gimana yah gan ?

mohon bantuannya

avatar ariflanitaman1
@ariflanitaman1

21 Kontribusi 0 Poin

Dipost 6 tahun yang lalu

Login untuk ikut Jawaban