search
HomeJavaJavaBaseJava determines what day it is today

Java determines what day it is today

Nov 22, 2019 pm 03:19 PM
java

Java determines what day it is today

How to determine the day of the week in Java:

1. Use the Calendar class

 /**
     * 获取当前日期是星期几<br>
     * 
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0){
            w = 0;
       }

        return weekDays[w];
    }

The weekday above is the day of the week, but in Java The trick is weekday=1, the day is Sunday; weekday=2, the day is Monday;...; weekday=7, the day is Saturday.

This is different from JS in which 0 is Sunday, 1 is Monday, 2 is Tuesday,..., 6 is Saturday. Therefore, based on JS experience, similar programs in Java will go wrong. .

2. Use SimpleDateFormat to format the date

import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * Created by zhisheng_tian on 2018/6/19
 */
public class FormatDateTime {
    public static void main(String[] args) {
        SimpleDateFormat myFmt3 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
        Date now = new Date();
        System.out.println(myFmt3.format(now));
  
    }
}

Output:

2018年06月19日 23时10分05秒 星期二

Note: The format string is case-sensitive

For creating SimpleDateFormat, pass in Parameters: EEEE represents the week, such as "Thursday"; MMMM represents the Chinese month, such as "November"; MM represents the month, such as "11";

yyyy represents the year, such as "2010"; dd represents Days, such as "25"

For more java knowledge, please pay attention tojava basic tutorial.

The above is the detailed content of Java determines what day it is today. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version