Home > Java > javaTutorial > How to get the day of the week based on date in java

How to get the day of the week based on date in java

王林
Release: 2023-04-29 08:25:06
forward
2816 people have browsed it

Method 1,
public static String getFullDateWeekTime(String sDate){
try{
String formater = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat format = new SimpleDateFormat (formater);
Date date=format.parse(sDate);
format.applyPattern("yyyy-MM-dd E HH:mm:ss");
return format.format(date);
}catch(Exception ex){
System.out.println("TimeUtil getFullDateWeekTime" ex.getMessage());
return "";
}
}

[@more@]Method 2,

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

public class Test {
public static void main(String[] args) {
final String dayNames[] = { "Sunday", "Monday", "Tuesday" ", "Wednesday", "Thursday", "Friday",
"Saturday" };

String s = "2006-01-12 16:30";
SimpleDateFormat sdfInput = new SimpleDateFormat ("yyyy-MM-dd HH:mm");

Calendar calendar = Calendar.getInstance();
Date date = new Date();

try {
date = sdfInput.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}

calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1;
if(dayOfWeek<0)dayOfWeek=0;
System.out.println(dayNames[dayOfWeek]);
}
}

The above is the detailed content of How to get the day of the week based on date in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template