分类 Java 下的文章
Java 时间(格式化的时间格式) 转 星期几
学习过程中的笔记,可能有不严谨的地方
Java 时间(格式化的时间格式) 转 星期几
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class Test {
public static void main(String[] args) throws ParseException {
System.out.println(strToWeek("2020-10-19 15:11:20", null));
}
/**
* 格式化的时间字符串 转 星期几
* @param date 例如 2020-10-19 15:46:32
* @param format 与 date 参数对应的格式,yyyy-MM-dd HH:mm:ss
* @return
*/
public static String strToWeek(String date, String format) {
if (format == null || format.isEmpty()) {
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E", Locale.CHINA);
try {
return simpleDateFormat.format(
(new SimpleDateFormat(format)).parse(date)
);
} catch (ParseException e) {
return "error";
}
}
}
关于 List 删除元素的笔记
有以下代码
个人理解,可能有错
MiBean.java
public class MiBean {
private int id;
private String name;
public MiBean(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
List<MiBean> miBeanLists = new ArrayList<>();
miBeanLists.add(new MiBean(9, "小米 10"));
miBeanLists.add(new MiBean(8, "小米 10 Pro"));
miBeanLists.add(new MiBean(7, "小米 10 Ultra"));
miBeanLists.add(new MiBean(12, "Redmi K30 Pro"));
miBeanLists.add(new MiBean(11, "Redmi K30 5G"));
miBeanLists.add(new MiBean(10, "Redmi K30 4G"));
//我以为可以直接遍历,然后调用 remove 方法移除
for(int i=0; i<miBeanLists.size(); i++){
miBeanLists.remove(i);
}
//结果它只能删除部分
//究其原因,原来是当删除之后 i<miBeanLists.size() 会重新判断
//因为我们之前删除了一个,所以在删除之后加一句 i--; 就可以全部删除了
//修改后的代码如下
for(int i=0; i<miBeanLists.size(); i++){
miBeanLists.remove(i);
i--;
}
结论
究其原因,原来是当删除之后
i<miBeanLists.size()
会重新判断因为我们之前删除了一个,所以在删除之后加一句 i—; 就可以全部删除了
复制别人总结的
在代码中,删除元素后,需要把下标减一。这是因为在每次删除元素后,ArrayList会将后面部分的元素依次往上挪一个位置(就是copy),所以,下一个需要访问的下标还是当前下标,所以必须得减一才能把所有元素都遍历完
使用 GsonFormat 解析数组形式的 json
记下笔记,以访忘记
假设有以下数据
[{
"CarId": 1,
"Time": "2017-11-26 16:58:11",
"Cost": 10
}, {
"CarId": 1,
"Time": "2017-11-26 16:58:19",
"Cost": 20
}, {
"CarId": 1,
"Time": "2017-11-26 16:58:24",
"Cost": 30
}, {
"CarId": 1,
"Time": "2017-11-26 16:58:28",
"Cost": 40
}]
以下类
public class GetCarAccountRecord {
/**
* CarId : 1
* Time : 2017-11-26 04:58:11
* Cost : 10
*/
private int CarId;
private String Time;
private int Cost;
public int getCarId() {
return CarId;
}
public void setCarId(int CarId) {
this.CarId = CarId;
}
public String getTime() {
return Time;
}
public void setTime(String Time) {
this.Time = Time;
}
public int getCost() {
return Cost;
}
public void setCost(int Cost) {
this.Cost = Cost;
}
}
使用 Gson 解析,然后遍历输出
List<GetCarAccountRecord> list = new Gson().fromJson(jsonObject.optJSONArray("ROWS_DETAIL").toString(), new TypeToken<List<GetCarAccountRecord>>() {}.getType());
for (GetCarAccountRecord getCarAccountRecord : list) {
Log.i("小车编号:", String.valueOf(getCarAccountRecord.getCarId()));
Log.i("充值金额:", String.valueOf(getCarAccountRecord.getCost()));
}
Java 创建子线程,使它立即开始执行
public class InvokeRun extends Thread {
private int i;
@Override
public void run() {
for(; i<100; i++){
System.out.println("["+Thread.currentThread().getName()+"]\t"+i);
}
}
public static void main(String[] args) throws Exception{
for(int i=0; i<100; i++){
System.out.println(Thread.currentThread().getName()+"\t"+i);
if(i == 20){
new InvokeRun().start();
new InvokeRun().start();
//让当前主线程休眠 1ms
//它会去执行另一个就绪状态的线程
//这样的话,子线程就可以立即开始执行
Thread.sleep(1);
}
}
}
}