下面的代码给出了当前时间.但它没有告诉任何关于毫秒的事情.
public static String getCurrentTimeStamp() {
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//dd/MM/yyyy
Date now = new Date();
String strDate = sdfDate.format(now);
return strDate;
}
Run Code Online (Sandbox Code Playgroud)
我得到格式的日期2009-09-22 16:47:08 (YYYY-MM-DD HH:MI:Sec).
但我想以格式2009-09-22 16:47:08.128((YYYY-MM-DD HH:MI:Sec.Ms)检索当前时间- 其中128表示毫秒.
SimpleTextFormat会很好的.这里最低的时间单位是秒,但我如何获得毫秒?
我想编写一个JavaScript函数,它将执行系统shell命令(ls例如)并返回值.
我该如何实现这一目标?
我曾经习惯copydir复制目录树但不推荐使用它.我的目录包含一些子目录,其中一些包含文件,另一些包含更多子目录.
我怎样才能复制整棵树?
我发现,无论是fill_parent和match_parent意味着同样的事情
我发现的唯一区别fill_parent是从API级别8开始被弃用并被替换为match_parent
但是,我没有注意到这两者之间有任何区别.如果两者都相同,那么为什么会被fill_parent弃用.任何人都可以解释这两者之间的任何差异,除了一个被弃用而另一个不被弃用的事实?
我已经浏览了http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
我读到了有关属性和资源包的内容.但我无法区分这些.何时使用Properties文件以及何时使用Resource bundle.
要加载属性文件,请使用以下代码
Properties tempProp = new Properties();
FileInputStream propsFile = new FileInputStream(xyz.properties);
tempProp.load(propsFile);
Run Code Online (Sandbox Code Playgroud)
加载资源包
ResourceBundle labels =
ResourceBundle.getBundle("xyz", currentLocale);
Enumeration bundleKeys = labels.getKeys();
Run Code Online (Sandbox Code Playgroud)
在这两种情况下(在资源包和Properites中)我们都使用属性文件.我发现的一个区别是,为了存储我们使用属性文件的应用程序特定数据,并使用i18n数据,我们使用资源包.我不知道我是对的.
我想知道上面两个的用法.这两者有什么区别.
我创建了一个JAR文件.现在,我创建了另一个Java程序.我想在其他目录中解压缩该JAR文件,这意味着我想做一些像解压缩这样的事情.
如果我运行jar -xf filename.jar这会导致一些错误:
Exception in thread "main" java.io.IOException: Cannot run program "jar":
java.io.IOException: error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at java.lang.Runtime.exec(Runtime.java:593)`
Run Code Online (Sandbox Code Playgroud) 我想通过套接字通道传输序列化对象.我想把"好朋友"字符串作为序列化对象,然后在套接字通道中写入此对象,而在另一端我想读取相同的对象并检索数据.
我想用Java做的所有这些事情SocketChannel.这该怎么做?我尝试过如下,但没有在接收方获得任何数据.
private static void writeObject(Object obj, SelectionKey selectionKey) {
ObjectOutputStream oos;
try {
SocketChannel channel = (SocketChannel) selectionKey.channel();
oos = new ObjectOutputStream(Channels.newOutputStream(channel));
oos.writeObject(obj);
} catch (IOException ex) {
ex.printStackTrace();
}
}
private static Object readObject(SelectionKey selectionKey) {
ObjectInputStream ois;
Object obj = new Object();
SocketChannel channel = (SocketChannel) selectionKey.channel();
try {
ois = new ObjectInputStream(Channels.newInputStream(channel));
obj = ois.readObject();
} catch (Exception ex) {
ex.printStackTrace();
}
return obj;
}
Run Code Online (Sandbox Code Playgroud) 对于字符串连接,我们可以使用concat()或concat运算符(+).
我尝试了以下性能测试,发现concat()字符串连接速度更快,内存效率更高.
字符串连接比较100,000次:
String str = null;
//------------Using Concatenation operator-------------
long time1 = System.currentTimeMillis();
long freeMemory1 = Runtime.getRuntime().freeMemory();
for(int i=0; i<100000; i++){
str = "Hi";
str = str+" Bye";
}
long time2 = System.currentTimeMillis();
long freeMemory2 = Runtime.getRuntime().freeMemory();
long timetaken1 = time2-time1;
long memoryTaken1 = freeMemory1 - freeMemory2;
System.out.println("Concat operator :" + "Time taken =" + timetaken1 +
" Memory Consumed =" + memoryTaken1);
//------------Using Concat method-------------
long time3 = System.currentTimeMillis();
long freeMemory3 …Run Code Online (Sandbox Code Playgroud) 我使用以下方法将任何原始数据类型转换为字符串
例
int i = 5;//
String convertToString = ""+i;// convert any primitive data type to string
Run Code Online (Sandbox Code Playgroud)
要将int数据类型转换为字符串,我可以使用Integer.toString()但是将任何类型的原始数据(不仅是int)类型转换为字符串的更好方法是什么