Java implementation to determine the day of the week based on date
Knowledge Supplement:
SimpleDateFormat
is a specific class that formats and analyzes data in a particularly sensitive way. It allows formatting (date -> text), parsing (text -> date) and normalization.
parse
Method: Parse the string type (java.lang.String) into the date type (java.util.Date).
Learning video sharing: java video tutorial
Examples are as follows:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateTools { public static String dateToWeek(String datetime) { SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; Calendar cal = Calendar.getInstance(); // 获得一个日历 Date datet = null; try { datet = f.parse(datetime); cal.setTime(datet); } catch (ParseException e) { e.printStackTrace(); } int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。 if (w < 0) w = 0; return weekDays[w]; } public static void main(String[] args) { System.out.println(DateTools.dateToWeek("2017-01-01")); } }
java related article tutorials: java introduction
The above is the detailed content of Java implementation to determine the day of the week based on date. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function