c# to java

2018-07-25  本文已影响0人  A_1341

列表

java中List是接口, 一般使用的实现是ArrayList
c#:List<T>
java:ArrayList<T>

字典

c#:Dictionary<TKey, TValue>
java:HashMap<TKey, TValue>

时间

c#:new DateTime()
java:

//获取now
new Date();//java.util.Date 不建议使用
Calendar.getInstance();//java.util.Calendar 不建议使用
new DateTime();//joda-time 第三方库,可以使用
Instant.now();//java.time.Instant java8加入
Timestamp.from(Instant.now());//java.sql.Timestamp 数据库用时间戳类型

//推荐最佳实践
LocalDateTime.now();//java.time.LocalDateTime 
Timestamp.valueOf(LocalDateTime.now());
LocalDateTime customLocalDataTime = LocalDateTime.of(2018,01,01,0,0,0);

//格式化
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd HH:mm"))

https://stackoverflow.com/questions/40075780/java-best-practice-for-date-manipulation-storage-for-geographically-diverse-user
https://stackoverflow.com/questions/5175728/how-to-get-the-current-date-time-in-java
https://stackoverflow.com/questions/42766674/java-convert-java-time-instant-to-java-sql-timestamp-without-zone-offset

//c#
private readonly object syncLock = new object();
public void SomeMethod() {
    lock(syncLock) { /* code */ }
}
//java
public synchronized void doImportantStuff() {
   // dangerous code goes here.
}

public void doImportantStuff() {
   // trivial stuff

   synchronized(someLock) {
      // dangerous code goes here.
   }
}

java编码样式

类名,接口,枚举首字母大写. 属性,变量,方法名小写 GOOGLE JAVA 程式風格指南 英文版

class AllTheColorsOfTheRainbow {
int anIntegerRepresentingColors;
void changeTheHueOfTheColor(int newHue) {
// ...
}
// ...
}
上一篇 下一篇

猜你喜欢

热点阅读