public class VeDate {
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatterparse(strDate, pos);
return strtodate;
}
/ //
@param sdate
@return
/
public static String getWeek(String sdate) {
// 再转换为时间
Date date = VeDatestrToDate(sdate);
Calendar c = CalendargetInstance();
csetTime(date);
// int hour=cget(CalendarDAY_OF_WEEK);
// hour中存的就是星期几了,其范围 1~7
// 1=星期日 7=星期六,其他类推
return new SimpleDateFormat("EEEE")format(cgetTime());
}
public static void main(String[] args){
Systemoutprintln(getWeek("2013-11-12"));
}
}
public static void main(String[] agrs) {
String newtime="2013-7-21";
Systemoutprint(testDate(newtime));
}
public static String testDate(String newtime) {
String dayNames[] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
Calendar c = CalendargetInstance();// 获得一个日历的实例
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
csetTime(sdfparse(newtime));
} catch (ParseException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
return dayNames[cget(CalendarDAY_OF_WEEK)-1];
}
对于单双数日,能被2整除就是双数日,反之是单数日
Calendar c_begin = CalendargetInstance();
Calendar c_end = CalendargetInstance();
c_beginset(2010, 3, 1); //Calendar的月从0-11,所以4月是3
c_endset(2010, 4, 20); //Calendar的月从0-11,所以5月是4
int count = 0;
while(c_beginbefore(c_end)){
//Systemoutprintln(new javasqlDate(c_begingetTime()getTime()));
if(c_beginget(CalendarDAY_OF_WEEK)==CalendarMONDAY){
count++;
}
if(count == 2){
Systemoutprintln(c_beginget(CalendarYEAR)+"年"+(c_beginget(CalendarMONTH)+1)+"月"+c_beginget(CalendarDATE)+"日");
break;
}
c_beginadd(CalendarDAY_OF_YEAR, 1);
}
可以这样试试,应该能理解吧。如有不懂再问我
这个问题不是很难,有关判断日期是否合法,其实已经有现成的包可以用,没有必要自己写方法来实现,代码如下:
import javautil;
public class WeekTest {
private String[][] javadays=new String[12][31];
public WeekTest(){
String[] weeks=new String[]{"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
for(int i=0;i<12;i++){
for(int j=0;j<31;j++){
GregorianCalendar gc=new GregorianCalendar();
gcsetLenient(false);
try{
gcset(2008,i,j+1);
int week=gcget(CalendarDAY_OF_WEEK);
javadays[i][j]=new String(weeks[week-1]);
}catch(Exception e){}
}
}
}
public void randomPrint(){
HashSet<Integer> hs=new HashSet<Integer>();
while(true){
if(hssize()==10)
break;
int day=((int)(Mathrandom()1000))%366;
if(hscontains(new Integer(day)))
continue;
hsadd(new Integer(day));
}
for(Integer day:hs){
GregorianCalendar gc=new GregorianCalendar(2008,0,1);
gcadd(CalendarDATE,dayintValue());
int month=gcget(CalendarMONTH)+1;
int date=gcget(CalendarDAY_OF_MONTH);
Systemoutprintln("2008年"+(month<10"0"+month:month)+"月"+(date<10"0"+date:date)+"日\t"+javadays[month-1][date-1]);
}
}
public void replaceAll(){
String[][] weekStr=new String[2][];
weekStr[0]=new String[]{"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
weekStr[1]=new String[]{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
for(int i=0;i<12;i++){
for(int j=0;j<31;j++){
for(int k=0;k<7;k++){
if(javadays[i][j]!=null && javadays[i][j]equals(weekStr[0][k]))
javadays[i][j]=new String(weekStr[1][k]);
}
}
}
}
public static void main(String[] args) {
WeekTest wt=new WeekTest();
wtrandomPrint();
wtreplaceAll();
wtrandomPrint();
}
}
import javautil;
public class getWeekDay {
/
@param args
/
public static void main(String[] args) {
// TODO Auto-generated method stub
new getWeekDay()launch(args);
}
void launch(String[] args){
if(argslength <3){
Systemoutprintln("Usage:getWeekDay Year Month Day");
Systemexit(0);
}
int year = IntegerparseInt(args[0]);
int month = IntegerparseInt(args[1]);
int day = IntegerparseInt(args[2]);
Calendar c = CalendargetInstance();
cset(year,month-1,day);
int week = cget(CalendarDAY_OF_WEEK);
Systemoutprintln(week-1);
}
}
一个简单的程序,你要封装成类的话,稍改一下就是了。
1,获取整年的天数
2,获取整月的天数
3,将信息输出
4,效果如下
5,博客地址参考:>
以上就是关于java写一个输入日期转换为星期的程序全部的内容,包括:java写一个输入日期转换为星期的程序、Java 日期如何判断是星期几求大神代码、Java根据日期得到当月第几周等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)