Java基础(有意思的程序)
程序员文章站
2024-03-12 22:47:20
...
可视化日历
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class VisialCalendar {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入日期(yyyy-MM-dd)");
String input = scanner.nextLine();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = df.parse(input);
Calendar c = new GregorianCalendar();
c.setTime(date);
int day = c.get(c.DATE);
c.set(c.DATE,1);
// System.out.println(c.get(c.DAY_OF_WEEK));//1号的星期数
System.out.println("日\t一\t二\t三\t四\t五\t六");
for(int i=0;i<c.get(c.DAY_OF_WEEK)-1;i++){
System.out.print('\t');
}
for(int j=1;j<=c.getActualMaximum(c.DATE);j++){
if(j == day){
System.out.print("*");
}
System.out.print(j+"\t");
if(c.get(c.DAY_OF_WEEK)==c.SATURDAY){
System.out.println('\n');
}
c.add(c.DATE,1);
}
// System.out.println(c.getActualMaximum(c.DATE));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
//2019-10-25
输出文件夹下的所有文件包括子文件夹
import java.io.File;
public class FileTree {
public static void printFile(File file,int level){
for(int i=0;i<level;i++){
System.out.print("-");
}
System.out.println(file.getName());
if(file.isDirectory()){
File[]files = file.listFiles();
for(File file1 :files){
printFile(file1,level+1);
}
}
}
public static void main(String[] args) {
File f = new File("targetFilesPath");
printFile(f,0);
}
}