Home  >  Article  >  Java  >  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

王林
王林forward
2023-04-29 08:25:062708browse

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete